Skip to content

Commit 6deb42c

Browse files
committed
Fix smooth scrolling in Safari 6 and Mobile Safari.
* Assume body is scrollable (when in jQuery obj), if no others are. * Fix kswedberg#29 and kswedberg#30.
1 parent d23197a commit 6deb42c

File tree

2 files changed

+36
-149
lines changed

2 files changed

+36
-149
lines changed

demo/stop.html

Lines changed: 0 additions & 120 deletions
This file was deleted.

src/jquery.smooth-scroll.js

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,28 @@ var version = '@VERSION',
3131
// if scroll(Top|Left) === 0, nudge the element 1px and see if it moves
3232
el[dir](1);
3333
scrolled = el[dir]() > 0;
34-
// then put it back, of course
35-
el[dir](0);
3634
if ( scrolled ) {
3735
scrollable.push(this);
3836
}
37+
// then put it back, of course
38+
el[dir](0);
3939
}
4040
});
4141

42-
if ( opts.el === 'first' && scrollable.length ) {
42+
// If no scrollable elements, fall back to <body>,
43+
// if it's in the jQuery collection
44+
// (doing this because Safari sets scrollTop async,
45+
// so can't set it to 1 and immediately get the value.)
46+
if (!scrollable.length) {
47+
this.each(function(index) {
48+
if (this.nodeName === 'BODY') {
49+
scrollable = [this];
50+
}
51+
});
52+
}
53+
54+
// Use the first scrollable element if we're calling firstScrollable()
55+
if ( opts.el === 'first' && scrollable.length > 1 ) {
4356
scrollable = [ scrollable[0] ];
4457
}
4558

@@ -104,7 +117,6 @@ $.fn.extend({
104117
});
105118

106119
return this;
107-
108120
}
109121
});
110122

@@ -115,7 +127,6 @@ $.smoothScroll = function(options, px) {
115127
scrollDir = 'scrollTop',
116128
aniProps = {},
117129
aniOpts = {},
118-
useScrollTo = false,
119130
scrollprops = [];
120131

121132
if ( typeof options === 'number') {
@@ -144,44 +155,40 @@ $.smoothScroll = function(options, px) {
144155
scrollerOffset = $scroller[scrollDir]();
145156
} else {
146157
$scroller = $('html, body').firstScrollable();
147-
useScrollTo = isTouch && 'scrollTo' in window;
148158
}
149159

150160
aniProps[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
151161

152162
opts.beforeScroll.call($scroller, opts);
153163

154-
if ( useScrollTo ) {
155-
scrollprops = (opts.direction == 'left') ? [aniProps[scrollDir], 0] : [0, aniProps[scrollDir]];
156-
window.scrollTo.apply(window, scrollprops);
157-
opts.afterScroll.call(opts.link, opts);
164+
speed = opts.speed;
158165

159-
} else {
160-
speed = opts.speed;
166+
// automatically calculate the speed of the scroll based on distance / coefficient
167+
if (speed === 'auto') {
161168

162-
// automatically calculate the speed of the scroll based on distance / coefficient
163-
if (speed === 'auto') {
169+
// if aniProps[scrollDir] == 0 then we'll use scrollTop() value instead
170+
speed = aniProps[scrollDir] || $scroller.scrollTop();
164171

165-
// if aniProps[scrollDir] == 0 then we'll use scrollTop() value instead
166-
speed = aniProps[scrollDir] || $scroller.scrollTop();
172+
// divide the speed by the coefficient
173+
speed = speed / opts.autoCoefficent;
174+
}
167175

168-
// divide the speed by the coefficient
169-
speed = speed / opts.autoCoefficent;
176+
aniOpts = {
177+
duration: speed,
178+
easing: opts.easing,
179+
complete: function() {
180+
opts.afterScroll.call(opts.link, opts);
170181
}
182+
};
171183

172-
aniOpts = {
173-
duration: speed,
174-
easing: opts.easing,
175-
complete: function() {
176-
opts.afterScroll.call(opts.link, opts);
177-
}
178-
};
179-
180-
if (opts.step) {
181-
aniOpts.step = opts.step;
182-
}
184+
if (opts.step) {
185+
aniOpts.step = opts.step;
186+
}
183187

188+
if ($scroller.length) {
184189
$scroller.stop().animate(aniProps, aniOpts);
190+
} else {
191+
opts.afterScroll.call(opts.link, opts);
185192
}
186193
};
187194

0 commit comments

Comments
 (0)