Skip to content

Commit 173dee5

Browse files
author
Karl Swedberg
committed
added changes that I had made previously. still not sure where they came from or when I made them
1 parent f501725 commit 173dee5

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

jquery.smooth-scroll.js

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
// Animated Scrolling for Same-Page Links
33
// @see http://www.learningjquery.com/2007/10/improved-animated-scrolling-script-for-same-page-links
44

5-
$.fn.smoothScroll = function(options) {
5+
$.fn.extend({
6+
smoothScroll: function(options) {
67
var opts = $.extend({}, $.fn.smoothScroll.defaults, options),
78
locationPath = filterPath(location.pathname),
89
scrollElem = scrollableElement('html', 'body');
@@ -15,7 +16,11 @@ $.fn.smoothScroll = function(options) {
1516
thisHash = link.hash && link.hash.replace('#',''),
1617
scrollTargetExists = thisHash && !!$('#' + thisHash).length;
1718

18-
if (hostMatch && pathMatch && scrollTargetExists) {
19+
if (opts.scrollTarget) {
20+
if (!/#\S$/.test(opts.scrollTarget)) { opts.scrollTarget += ':first'; }
21+
$link.data('scrollTarget', opts.scrollTarget);
22+
23+
} else if (hostMatch && pathMatch && scrollTargetExists) {
1924
var include = true,
2025

2126
exclude = opts.exclude,
@@ -41,22 +46,14 @@ $.fn.smoothScroll = function(options) {
4146
$link.data('scrollTarget', '#' + thisHash);
4247
}
4348
}
44-
4549
});
4650

47-
51+
4852
this.die('click.smoothscroll').live('click.smoothscroll', function(event) {
49-
var scrollTargetId = $(this).data('scrollTarget');
50-
if (scrollTargetId) {
51-
event.preventDefault();
52-
53-
var scrollTargetOffset = $(scrollTargetId).offset().top;
54-
55-
$(scrollElem).animate({scrollTop: scrollTargetOffset + opts.offset}, 400, function() {
56-
// location.hash = target;
57-
});
58-
}
53+
event.preventDefault();
54+
$(this).smoothScroller();
5955
});
56+
6057
return this;
6158

6259
// private functions
@@ -92,13 +89,38 @@ $.fn.smoothScroll = function(options) {
9289
window.console.log($obj);
9390
}
9491
}
95-
};
92+
},
93+
94+
smoothScroller: function(link) {
95+
var scrollTargetId = $(link).data('scrollTarget');
96+
if (scrollTargetId) {
97+
event.preventDefault();
98+
99+
var scrollTargetOffset = $(scrollTargetId).offset().top;
100+
101+
$(scrollElem).animate({
102+
scrollTop: scrollTargetOffset + opts.offset
103+
},
104+
{
105+
duration: opts.speed,
106+
easing: opts.easing
107+
}, function() {
108+
opts.afterScroll.call(link);
109+
});
110+
}
111+
}
112+
});
96113

97114
// default options
98115
$.fn.smoothScroll.defaults = {
116+
autobind: true,
99117
exclude: [],
100118
excludeWithin:[],
101-
offset: 0
119+
offset: 0,
120+
scrollTarget: null, // only use if you want to override default behavior
121+
afterScroll: null, // function to be called after window is scrolled
122+
easing: 'swing',
123+
speed: 400
102124
};
103125

104126
})(jQuery);

0 commit comments

Comments
 (0)