Skip to content

Commit 6c616e1

Browse files
author
Karl Swedberg
committed
fixed afterScroll callback function and using $.smoothScroll() -- as opposed to $(selector).smoothScroll() -- with options and px.
1 parent 50c6d54 commit 6c616e1

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

demo/index.html

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
<script src="../jquery.smooth-scroll.js" type="text/javascript"></script>
1414
<script type="text/javascript">
1515
$(document).ready(function() {
16-
$('li a').smoothScroll();
16+
$('li a').smoothScroll({
17+
afterScroll: function() {
18+
/* location.hash = this.hash; */
19+
}
20+
});
1721
});
1822

1923
</script>

jquery.smooth-scroll.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ $.fn.extend({
1111

1212
this.each(function() {
1313
var opts = $.extend({}, $.fn.smoothScroll.defaults, options);
14+
1415
$(this).bind('click', function(event) {
1516
var link = this, $link = $(this),
1617
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
@@ -39,6 +40,7 @@ $.fn.extend({
3940

4041
if (include) {
4142
opts.scrollTarget = opts.scrollTarget || thisHash;
43+
opts.link = link;
4244
event.preventDefault();
4345
$.smoothScroll(opts);
4446
}
@@ -61,22 +63,27 @@ $.smoothScroll = function(options, px) {
6163
scrollTargetOffset = options;
6264
} else {
6365
opts = $.extend({}, $.fn.smoothScroll.defaults, options);
64-
scrollTargetOffset = $(opts.scrollTarget).offset().top;
66+
scrollTargetOffset = px || $(opts.scrollTarget).offset().top;
6567
}
66-
68+
opts = $.extend({link: null}, opts);
69+
6770
$(scrollElem).animate({
6871
scrollTop: scrollTargetOffset + opts.offset
6972
},
7073
{
7174
duration: opts.speed,
72-
easing: opts.easing
73-
}, function() {
74-
opts.afterScroll.call(link);
75+
easing: opts.easing,
76+
complete: function() {
77+
if ( opts.afterScroll && $.isFunction(opts.afterScroll) ) {
78+
opts.afterScroll.call(opts.link, opts);
79+
}
80+
}
7581
});
7682

7783
};
7884

7985
$.smoothScroll.version = version;
86+
8087
// default options
8188
$.fn.smoothScroll.defaults = {
8289
exclude: [],

0 commit comments

Comments
 (0)