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
5
3
*
4
+ * Date: Sat Nov 20 18:12:52 2010 -0500
5
+ * Requires: jQuery v1.3+
6
+ *
7
+ * Copyright 2010, Karl Swedberg
6
8
* Dual licensed under the MIT and GPL licenses (just like jQuery):
7
9
* http://www.opensource.org/licenses/mit-license.php
8
10
* http://www.gnu.org/licenses/gpl.html
9
11
*
10
- */
11
-
12
+ *
13
+ *
14
+ *
15
+ */
12
16
13
17
( function ( $ ) {
14
18
15
- var version = '1.2 ' ;
19
+ var version = '1.3 ' ;
16
20
17
21
var locationPath = filterPath ( location . pathname ) ;
18
22
@@ -24,22 +28,25 @@ $.fn.extend({
24
28
if ( this == document || this == window ) { return ; }
25
29
var el = $ ( this ) ;
26
30
if ( el . scrollTop ( ) > 0 ) {
27
- scrollable = [ this ] ;
28
- return false ;
31
+ scrollable . push ( this ) ;
32
+ return ;
29
33
}
30
34
31
35
el . scrollTop ( 1 ) ;
32
36
scrolled = el . scrollTop ( ) > 0 ;
33
37
el . scrollTop ( 0 ) ;
34
38
if ( scrolled ) {
35
- scrollable = [ this ] ;
36
- return false ;
39
+ scrollable . push ( this ) ;
40
+ return ;
37
41
}
38
42
39
43
} ) ;
40
44
41
45
return this . pushStack ( scrollable ) ;
42
46
} ,
47
+ firstScrollable : function ( ) {
48
+ return this . scrollable ( ) . eq ( 0 ) ;
49
+ } ,
43
50
44
51
smoothScroll : function ( options ) {
45
52
var opts = $ . extend ( { } , $ . fn . smoothScroll . defaults , options ) ;
@@ -48,11 +55,11 @@ $.fn.extend({
48
55
var link = this , $link = $ ( this ) ,
49
56
hostMatch = ( ( location . hostname === link . hostname ) || ! link . hostname ) ,
50
57
pathMatch = opts . scrollTarget || ( filterPath ( link . pathname ) || locationPath ) === locationPath ,
51
- thisHash = link . hash && '#' + link . hash . replace ( '#' , '' ) ,
58
+ thisHash = link . hash ,
52
59
include = true ;
53
60
54
61
55
- if ( ! opts . scrollTarget && ( ! hostMatch || ! pathMatch || thisHash . length == 1 ) ) {
62
+ if ( ! opts . scrollTarget && ( ! hostMatch || ! pathMatch || ! thisHash ) ) {
56
63
include = false ;
57
64
} else {
58
65
var exclude = opts . exclude , elCounter = 0 , el = exclude . length ;
@@ -85,7 +92,11 @@ $.fn.extend({
85
92
} ) ;
86
93
87
94
$ . 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 = { } ;
89
100
90
101
if ( typeof options === 'number' ) {
91
102
opts = $ . fn . smoothScroll . defaults ;
@@ -106,11 +117,14 @@ $.smoothScroll = function(options, px) {
106
117
}
107
118
opts = $ . extend ( { link : null } , opts ) ;
108
119
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
+ }
112
126
113
- aniprops [ 'scroll' + dirs [ opts . direction ] ] = scrollTargetOffset + opts . offset ;
127
+ aniprops [ 'scroll' + dirs [ opts . direction ] ] = scrollTargetOffset + scrollerOffset + opts . offset ;
114
128
$scroller . animate ( aniprops ,
115
129
{
116
130
duration : opts . speed ,
@@ -133,7 +147,7 @@ $.fn.smoothScroll.defaults = {
133
147
offset : 0 ,
134
148
direction : 'top' , // one of 'top' or 'left'
135
149
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.
137
151
scrollTarget : null , // only use if you want to override default behavior
138
152
afterScroll : null , // function to be called after window is scrolled. "this" is the triggering element
139
153
easing : 'swing' ,
@@ -149,5 +163,4 @@ function filterPath(string) {
149
163
. replace ( / \/ $ / , '' ) ;
150
164
}
151
165
152
-
153
166
} ) ( jQuery ) ;
0 commit comments