Skip to content

Commit 5c08756

Browse files
committed
.scrollable() method now keeps all scrollable elements that were part of the jQuery set.
New .firstScrollable() method selects the first scrollable element in the set. Added extra offset for scrollable elements other than html/body. Fixes issue kswedberg#2.
1 parent cfdc883 commit 5c08756

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

jquery.smooth-scroll.js

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
1-
/*
2-
* jQuery Smooth Scroll plugin
3-
* Version 1.2 (July 6, 2010)
4-
* @requires jQuery v1.3+
1+
/*!
2+
* jQuery Smooth Scroll Plugin v1.3
53
*
4+
* Date: Sat Nov 20 18:12:52 2010 -0500
5+
* Requires: jQuery v1.3+
6+
*
7+
* Copyright 2010, Karl Swedberg
68
* Dual licensed under the MIT and GPL licenses (just like jQuery):
79
* http://www.opensource.org/licenses/mit-license.php
810
* http://www.gnu.org/licenses/gpl.html
911
*
10-
*/
11-
12+
*
13+
*
14+
*
15+
*/
1216

1317
(function($) {
1418

15-
var version = '1.2';
19+
var version = '1.3';
1620

1721
var locationPath = filterPath(location.pathname);
1822

@@ -24,22 +28,25 @@ $.fn.extend({
2428
if (this == document || this == window) { return; }
2529
var el = $(this);
2630
if ( el.scrollTop() > 0 ) {
27-
scrollable = [this];
28-
return false;
31+
scrollable.push(this);
32+
return;
2933
}
3034

3135
el.scrollTop(1);
3236
scrolled = el.scrollTop() > 0;
3337
el.scrollTop(0);
3438
if ( scrolled ) {
35-
scrollable = [this];
36-
return false;
39+
scrollable.push(this);
40+
return;
3741
}
3842

3943
});
4044

4145
return this.pushStack(scrollable);
4246
},
47+
firstScrollable: function() {
48+
return this.scrollable().eq(0);
49+
},
4350

4451
smoothScroll: function(options) {
4552
var opts = $.extend({}, $.fn.smoothScroll.defaults, options);
@@ -48,11 +55,11 @@ $.fn.extend({
4855
var link = this, $link = $(this),
4956
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
5057
pathMatch = opts.scrollTarget || (filterPath(link.pathname) || locationPath) === locationPath,
51-
thisHash = link.hash && '#' + link.hash.replace('#',''),
58+
thisHash = link.hash,
5259
include = true;
5360

5461

55-
if (!opts.scrollTarget && (!hostMatch || !pathMatch || thisHash.length == 1) ) {
62+
if (!opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
5663
include = false;
5764
} else {
5865
var exclude = opts.exclude, elCounter = 0, el = exclude.length;
@@ -85,7 +92,11 @@ $.fn.extend({
8592
});
8693

8794
$.smoothScroll = function(options, px) {
88-
var opts, scrollTargetOffset, offPos = 'offset';
95+
var opts, $scroller, scrollTargetOffset,
96+
scrollerOffset = 0,
97+
offPos = 'offset',
98+
dirs = {top: 'Top', 'left': 'Left'},
99+
aniprops = {};
89100

90101
if ( typeof options === 'number') {
91102
opts = $.fn.smoothScroll.defaults;
@@ -106,11 +117,14 @@ $.smoothScroll = function(options, px) {
106117
}
107118
opts = $.extend({link: null}, opts);
108119

109-
var $scroller = opts.scrollElement || $('html, body').scrollable(),
110-
dirs = {top: 'Top', 'left': 'Left'},
111-
aniprops = {};
120+
if ( opts.scrollElement ) {
121+
$scroller = opts.scrollElement;
122+
scrollerOffset = $scroller.scrollTop();
123+
} else {
124+
$scroller = $('html, body').firstScrollable();
125+
}
112126

113-
aniprops['scroll' + dirs[opts.direction]] = scrollTargetOffset + opts.offset;
127+
aniprops['scroll' + dirs[opts.direction]] = scrollTargetOffset + scrollerOffset + opts.offset;
114128
$scroller.animate(aniprops,
115129
{
116130
duration: opts.speed,
@@ -133,7 +147,7 @@ $.fn.smoothScroll.defaults = {
133147
offset: 0,
134148
direction: 'top', // one of 'top' or 'left'
135149
scrollElement: null, // jQuery set of elements you wish to scroll.
136-
//if null (default), $('html, body').scrollable() is used.
150+
//if null (default), $('html, body').firstScrollable() is used.
137151
scrollTarget: null, // only use if you want to override default behavior
138152
afterScroll: null, // function to be called after window is scrolled. "this" is the triggering element
139153
easing: 'swing',
@@ -149,5 +163,4 @@ function filterPath(string) {
149163
.replace(/\/$/,'');
150164
}
151165

152-
153166
})(jQuery);

0 commit comments

Comments
 (0)