1
1
/*
2
2
* jQuery Smooth Scroll plugin
3
- * Version 1.1 (March 25 , 2010)
3
+ * Version 1.2 (July 6 , 2010)
4
4
* @requires jQuery v1.3+
5
5
*
6
6
* Dual licensed under the MIT and GPL licenses (just like jQuery):
12
12
13
13
( function ( $ ) {
14
14
15
- var version = '1.1 ' ;
15
+ var version = '1.2 ' ;
16
16
17
17
var locationPath = filterPath ( location . pathname ) ;
18
18
19
19
$ . fn . extend ( {
20
- smoothScroll : function ( options ) {
21
-
20
+ scrollable : function ( ) {
21
+ var scrollable = [ ] , scrolled = false ;
22
22
this . each ( function ( ) {
23
- var opts = $ . extend ( { } , $ . fn . smoothScroll . defaults , options ) ;
24
-
25
- $ ( this ) . bind ( 'click' , function ( event ) {
26
- var link = this , $link = $ ( this ) ,
27
- hostMatch = ( ( location . hostname === link . hostname ) || ! link . hostname ) ,
28
- pathMatch = opts . scrollTarget || ( filterPath ( link . pathname ) || locationPath ) === locationPath ,
29
- thisHash = link . hash && '#' + link . hash . replace ( '#' , '' ) ,
30
- include = true ;
31
-
32
-
33
- if ( ! opts . scrollTarget && ( ! hostMatch || ! pathMatch || thisHash . length == 1 ) ) {
34
- include = false ;
35
- } else {
36
- var exclude = opts . exclude , elCounter = 0 , el = exclude . length ;
37
- while ( include && elCounter < el ) {
38
- if ( $link . is ( exclude [ elCounter ++ ] ) ) {
39
- include = false ;
40
- }
41
- }
42
23
43
- var excludeWithin = opts . excludeWithin , ewlCounter = 0 , ewl = excludeWithin . length ;
44
- while ( include && ewlCounter < ewl ) {
45
- if ( $link . parents ( excludeWithin [ ewlCounter ++ ] + ':first' ) . length ) {
46
- include = false ;
47
- }
48
- }
24
+ if ( this == document || this == window ) { return ; }
25
+ var el = $ ( this ) ;
26
+ if ( el . scrollTop ( ) > 0 ) {
27
+ scrollable = [ this ] ;
28
+ return false ;
29
+ }
30
+
31
+ el . scrollTop ( 1 ) ;
32
+ scrolled = el . scrollTop ( ) > 0 ;
33
+ el . scrollTop ( 0 ) ;
34
+ if ( scrolled ) {
35
+ scrollable = [ this ] ;
36
+ return false ;
37
+ }
38
+
39
+ } ) ;
40
+
41
+ return this . pushStack ( scrollable ) ;
42
+ } ,
43
+
44
+ smoothScroll : function ( options ) {
45
+ var opts = $ . extend ( { } , $ . fn . smoothScroll . defaults , options ) ;
46
+ this . die ( 'click.smoothscroll' ) . live ( 'click.smoothscroll' , function ( event ) {
47
+
48
+ var link = this , $link = $ ( this ) ,
49
+ hostMatch = ( ( location . hostname === link . hostname ) || ! link . hostname ) ,
50
+ pathMatch = opts . scrollTarget || ( filterPath ( link . pathname ) || locationPath ) === locationPath ,
51
+ thisHash = link . hash && '#' + link . hash . replace ( '#' , '' ) ,
52
+ include = true ;
53
+
54
+
55
+ if ( ! opts . scrollTarget && ( ! hostMatch || ! pathMatch || thisHash . length == 1 ) ) {
56
+ include = false ;
57
+ } else {
58
+ var exclude = opts . exclude , elCounter = 0 , el = exclude . length ;
59
+ while ( include && elCounter < el ) {
60
+ if ( $link . is ( exclude [ elCounter ++ ] ) ) {
61
+ include = false ;
62
+ }
49
63
}
50
64
51
- if ( include ) {
52
- opts . scrollTarget = opts . scrollTarget || thisHash ;
53
- opts . link = link ;
54
- event . preventDefault ( ) ;
55
- $ . smoothScroll ( opts ) ;
65
+ var excludeWithin = opts . excludeWithin , ewlCounter = 0 , ewl = excludeWithin . length ;
66
+ while ( include && ewlCounter < ewl ) {
67
+ if ( $ link. closest ( excludeWithin [ ewlCounter ++ ] ) . length ) {
68
+ include = false ;
69
+ }
56
70
}
57
- } ) ;
71
+ }
72
+
73
+ if ( include ) {
74
+ opts . scrollTarget = opts . scrollTarget || thisHash ;
75
+ opts . link = link ;
76
+ event . preventDefault ( ) ;
77
+ $ . smoothScroll ( opts ) ;
78
+ }
58
79
} ) ;
59
-
80
+
60
81
return this ;
61
82
62
83
}
63
-
84
+
64
85
} ) ;
65
86
66
87
$ . smoothScroll = function ( options , px ) {
67
- var opts ,
68
- scrollTargetOffset ,
69
- scrollElem = scrollableElement ( 'html' , 'body' ) ;
70
-
88
+ var opts , scrollTargetOffset , offPos = 'offset' ;
89
+
71
90
if ( typeof options === 'number' ) {
72
91
opts = $ . fn . smoothScroll . defaults ;
73
92
scrollTargetOffset = options ;
74
93
} else {
75
- opts = $ . extend ( { } , $ . fn . smoothScroll . defaults , options ) ;
76
- scrollTargetOffset = px || $ ( opts . scrollTarget ) . offset ( ) && $ ( opts . scrollTarget ) . offset ( ) . top || 0 ;
94
+ opts = $ . extend ( { } , $ . fn . smoothScroll . defaults , options || { } ) ;
95
+ if ( opts . scrollElement ) {
96
+ offPos = 'position' ;
97
+ if ( opts . scrollElement . css ( 'position' ) == 'static' ) {
98
+ opts . scrollElement . css ( 'position' , 'relative' ) ;
99
+ }
100
+ }
101
+
102
+ scrollTargetOffset = px ||
103
+ ( $ ( opts . scrollTarget ) [ offPos ] ( ) &&
104
+ $ ( opts . scrollTarget ) [ offPos ] ( ) [ opts . direction ] ) ||
105
+ 0 ;
77
106
}
78
107
opts = $ . extend ( { link : null } , opts ) ;
79
-
80
- $ ( scrollElem ) . animate ( {
81
- scrollTop : scrollTargetOffset + opts . offset
82
- } ,
108
+
109
+ var $scroller = opts . scrollElement || $ ( 'html, body' ) . scrollable ( ) ,
110
+ dirs = { top : 'Top' , 'left' : 'Left' } ,
111
+ aniprops = { } ;
112
+
113
+ aniprops [ 'scroll' + dirs [ opts . direction ] ] = scrollTargetOffset + opts . offset ;
114
+ $scroller . animate ( aniprops ,
83
115
{
84
116
duration : opts . speed ,
85
- easing : opts . easing ,
117
+ easing : opts . easing ,
86
118
complete : function ( ) {
87
119
if ( opts . afterScroll && $ . isFunction ( opts . afterScroll ) ) {
88
120
opts . afterScroll . call ( opts . link , opts ) ;
@@ -99,35 +131,17 @@ $.fn.smoothScroll.defaults = {
99
131
exclude : [ ] ,
100
132
excludeWithin :[ ] ,
101
133
offset : 0 ,
134
+ direction : 'top' , // one of 'top' or 'left'
135
+ scrollElement : null , // jQuery set of elements you wish to scroll.
136
+ //if null (default), $('html, body').scrollable() is used.
102
137
scrollTarget : null , // only use if you want to override default behavior
103
138
afterScroll : null , // function to be called after window is scrolled. "this" is the triggering element
104
139
easing : 'swing' ,
105
140
speed : 400
106
141
} ;
107
142
108
143
109
- // private functions
110
-
111
- // don't pass window or document
112
- function scrollableElement ( els ) {
113
- for ( var i = 0 , argLength = arguments . length ; i < argLength ; i ++ ) {
114
- var el = arguments [ i ] ,
115
- $scrollElement = $ ( el ) ;
116
- if ( $scrollElement . scrollTop ( ) > 0 ) {
117
- return el ;
118
- } else {
119
- $scrollElement . scrollTop ( 1 ) ;
120
- var isScrollable = $scrollElement . scrollTop ( ) > 0 ;
121
- $scrollElement . scrollTop ( 0 ) ;
122
- if ( isScrollable ) {
123
- return el ;
124
- }
125
- }
126
- }
127
- return [ ] ;
128
- }
129
-
130
-
144
+ // private function
131
145
function filterPath ( string ) {
132
146
return string
133
147
. replace ( / ^ \/ / , '' )
0 commit comments