Skip to content

Commit 70e49e4

Browse files
committed
fixed a number of areas where the plugin wasn't respecting the direction (left vs top) set by the user
1 parent 53f4e57 commit 70e49e4

File tree

1 file changed

+50
-46
lines changed

1 file changed

+50
-46
lines changed

jquery.smooth-scroll.js

Lines changed: 50 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*!
2-
* jQuery Smooth Scroll Plugin v1.3
2+
* jQuery Smooth Scroll Plugin v1.4
33
*
44
* Date: Wed Dec 1 15:03:21 2010 -0500
55
* Requires: jQuery v1.3+
@@ -16,45 +16,58 @@
1616

1717
(function($) {
1818

19-
var version = '1.3.1';
20-
21-
var locationPath = filterPath(location.pathname);
22-
23-
function getScrollable(els) {
24-
var scrollable = [], scrolled = false;
19+
var version = '1.4',
20+
defaults = {
21+
exclude: [],
22+
excludeWithin:[],
23+
offset: 0,
24+
direction: 'top', // one of 'top' or 'left'
25+
scrollElement: null, // jQuery set of elements you wish to scroll (for $.smoothScroll).
26+
// if null (default), $('html, body').firstScrollable() is used.
27+
scrollTarget: null, // only use if you want to override default behavior
28+
afterScroll: null, // function to be called after window is scrolled. "this" is the triggering element
29+
easing: 'swing',
30+
speed: 400
31+
},
32+
locationPath = filterPath(location.pathname),
33+
getScrollable = function(opts) {
34+
var scrollable = [],
35+
scrolled = false,
36+
dir = opts.dir && opts.dir == 'left' ? 'scrollLeft' : 'scrollTop';
37+
38+
this.each(function() {
39+
40+
if (this == document || this == window) { return; }
41+
var el = $(this);
42+
if ( el[dir]() > 0 ) {
43+
scrollable.push(this);
44+
return;
45+
}
2546

26-
this.each(function() {
47+
el[dir](1);
48+
scrolled = el[dir]() > 0;
49+
el[dir](0);
50+
if ( scrolled ) {
51+
scrollable.push(this);
52+
return;
53+
}
2754

28-
if (this == document || this == window) { return; }
29-
var el = $(this);
30-
if ( el.scrollTop() > 0 ) {
31-
scrollable.push(this);
32-
return;
33-
}
55+
});
3456

35-
el.scrollTop(1);
36-
scrolled = el.scrollTop() > 0;
37-
el.scrollTop(0);
38-
if ( scrolled ) {
39-
scrollable.push(this);
40-
return;
57+
if ( opts.el === 'first' && scrollable.length ) {
58+
scrollable = [ scrollable.shift() ];
4159
}
4260

43-
});
61+
return scrollable;
62+
};
4463

45-
if ( els === 'first' && scrollable.length ) {
46-
scrollable = [ scrollable.shift() ];
47-
}
48-
49-
return scrollable;
50-
}
5164
$.fn.extend({
52-
scrollable: function() {
53-
var scrl = getScrollable.call(this);
65+
scrollable: function(dir) {
66+
var scrl = getScrollable.call(this, {dir: dir});
5467
return this.pushStack(scrl);
5568
},
56-
firstScrollable: function() {
57-
var scrl = getScrollable.call(this, 'first');
69+
firstScrollable: function(dir) {
70+
var scrl = getScrollable.call(this, {el: 'first', dir: dir});
5871
return this.pushStack(scrl);
5972
},
6073

@@ -106,7 +119,7 @@ $.smoothScroll = function(options, px) {
106119
var opts, $scroller, scrollTargetOffset,
107120
scrollerOffset = 0,
108121
offPos = 'offset',
109-
dirs = {top: 'Top', 'left': 'Left'},
122+
scrollDir = 'scrollTop',
110123
aniprops = {};
111124

112125
if ( typeof options === 'number') {
@@ -127,15 +140,17 @@ $.smoothScroll = function(options, px) {
127140
0;
128141
}
129142
opts = $.extend({link: null}, opts);
143+
scrollDir = opts.direction == 'left' ? 'scrollLeft' : scrollDir;
130144

131145
if ( opts.scrollElement ) {
132146
$scroller = opts.scrollElement;
133-
scrollerOffset = $scroller.scrollTop();
147+
scrollerOffset = $scroller[scrollDir]();
134148
} else {
135149
$scroller = $('html, body').firstScrollable();
136150
}
137151

138-
aniprops['scroll' + dirs[opts.direction]] = scrollTargetOffset + scrollerOffset + opts.offset;
152+
aniprops[scrollDir] = scrollTargetOffset + scrollerOffset + opts.offset;
153+
139154
$scroller.animate(aniprops,
140155
{
141156
duration: opts.speed,
@@ -152,18 +167,7 @@ $.smoothScroll = function(options, px) {
152167
$.smoothScroll.version = version;
153168

154169
// default options
155-
$.fn.smoothScroll.defaults = {
156-
exclude: [],
157-
excludeWithin:[],
158-
offset: 0,
159-
direction: 'top', // one of 'top' or 'left'
160-
scrollElement: null, // jQuery set of elements you wish to scroll.
161-
//if null (default), $('html, body').firstScrollable() is used.
162-
scrollTarget: null, // only use if you want to override default behavior
163-
afterScroll: null, // function to be called after window is scrolled. "this" is the triggering element
164-
easing: 'swing',
165-
speed: 400
166-
};
170+
$.fn.smoothScroll.defaults = defaults;
167171

168172

169173
// private function

0 commit comments

Comments
 (0)