1
1
/*!
2
- * jQuery Smooth Scroll Plugin v1.3
2
+ * jQuery Smooth Scroll Plugin v1.4
3
3
*
4
4
* Date: Wed Dec 1 15:03:21 2010 -0500
5
5
* Requires: jQuery v1.3+
16
16
17
17
( function ( $ ) {
18
18
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
+ }
25
46
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
+ }
27
54
28
- if ( this == document || this == window ) { return ; }
29
- var el = $ ( this ) ;
30
- if ( el . scrollTop ( ) > 0 ) {
31
- scrollable . push ( this ) ;
32
- return ;
33
- }
55
+ } ) ;
34
56
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 ( ) ] ;
41
59
}
42
60
43
- } ) ;
61
+ return scrollable ;
62
+ } ;
44
63
45
- if ( els === 'first' && scrollable . length ) {
46
- scrollable = [ scrollable . shift ( ) ] ;
47
- }
48
-
49
- return scrollable ;
50
- }
51
64
$ . fn . extend ( {
52
- scrollable : function ( ) {
53
- var scrl = getScrollable . call ( this ) ;
65
+ scrollable : function ( dir ) {
66
+ var scrl = getScrollable . call ( this , { dir : dir } ) ;
54
67
return this . pushStack ( scrl ) ;
55
68
} ,
56
- firstScrollable : function ( ) {
57
- var scrl = getScrollable . call ( this , 'first' ) ;
69
+ firstScrollable : function ( dir ) {
70
+ var scrl = getScrollable . call ( this , { el : 'first' , dir : dir } ) ;
58
71
return this . pushStack ( scrl ) ;
59
72
} ,
60
73
@@ -106,7 +119,7 @@ $.smoothScroll = function(options, px) {
106
119
var opts , $scroller , scrollTargetOffset ,
107
120
scrollerOffset = 0 ,
108
121
offPos = 'offset' ,
109
- dirs = { top : 'Top' , 'left' : 'Left' } ,
122
+ scrollDir = 'scrollTop' ,
110
123
aniprops = { } ;
111
124
112
125
if ( typeof options === 'number' ) {
@@ -127,15 +140,17 @@ $.smoothScroll = function(options, px) {
127
140
0 ;
128
141
}
129
142
opts = $ . extend ( { link : null } , opts ) ;
143
+ scrollDir = opts . direction == 'left' ? 'scrollLeft' : scrollDir ;
130
144
131
145
if ( opts . scrollElement ) {
132
146
$scroller = opts . scrollElement ;
133
- scrollerOffset = $scroller . scrollTop ( ) ;
147
+ scrollerOffset = $scroller [ scrollDir ] ( ) ;
134
148
} else {
135
149
$scroller = $ ( 'html, body' ) . firstScrollable ( ) ;
136
150
}
137
151
138
- aniprops [ 'scroll' + dirs [ opts . direction ] ] = scrollTargetOffset + scrollerOffset + opts . offset ;
152
+ aniprops [ scrollDir ] = scrollTargetOffset + scrollerOffset + opts . offset ;
153
+
139
154
$scroller . animate ( aniprops ,
140
155
{
141
156
duration : opts . speed ,
@@ -152,18 +167,7 @@ $.smoothScroll = function(options, px) {
152
167
$ . smoothScroll . version = version ;
153
168
154
169
// 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 ;
167
171
168
172
169
173
// private function
0 commit comments