Skip to content

Commit 34cd172

Browse files
tu-vu-duykswedberg
tu-vu-duy
authored andcommitted
Fix value of 'speed' when set default is 'auto'. Closes kswedberg#33
1 parent 2682426 commit 34cd172

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/jquery.smooth-scroll.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ $.smoothScroll = function(options, px) {
158158
if ( options === 'options' && typeof px === 'object' ) {
159159
return $.extend(optionOverrides, px);
160160
}
161-
var opts, $scroller, scrollTargetOffset, speed,
161+
var opts, $scroller, scrollTargetOffset, speed, delta,
162162
scrollerOffset = 0,
163163
offPos = 'offset',
164164
scrollDir = 'scrollTop',
@@ -205,11 +205,15 @@ $.smoothScroll = function(options, px) {
205205
// automatically calculate the speed of the scroll based on distance / coefficient
206206
if (speed === 'auto') {
207207

208-
// if aniProps[scrollDir] == 0 then we'll use scrollTop() value instead
209-
speed = aniProps[scrollDir] || $scroller.scrollTop();
208+
// $scroller.scrollTop() is position before scroll, aniProps[scrollDir] is position after
209+
// When delta is greater, speed will be greater.
210+
delta = aniProps[scrollDir] - $scroller.scrollTop();
211+
if(delta < 0) {
212+
delta *= -1;
213+
}
210214

211-
// divide the speed by the coefficient
212-
speed = speed / opts.autoCoefficent;
215+
// Divide the delta by the coefficient
216+
speed = delta / opts.autoCoefficent;
213217
}
214218

215219
aniOpts = {

0 commit comments

Comments
 (0)