forked from kswedberg/jquery-smooth-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.smooth-scroll.js
177 lines (147 loc) · 4.51 KB
/
jquery.smooth-scroll.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*!
* jQuery Smooth Scroll Plugin v1.3
*
* Date: Wed Dec 1 15:03:21 2010 -0500
* Requires: jQuery v1.3+
*
* Copyright 2010, Karl Swedberg
* Dual licensed under the MIT and GPL licenses (just like jQuery):
* http://www.opensource.org/licenses/mit-license.php
* http://www.gnu.org/licenses/gpl.html
*
*
*
*
*/
(function($) {
var version = '1.3.1';
var locationPath = filterPath(location.pathname);
function getScrollable(els) {
var scrollable = [], scrolled = false;
this.each(function() {
if (this == document || this == window) { return; }
var el = $(this);
if ( el.scrollTop() > 0 ) {
scrollable.push(this);
return;
}
el.scrollTop(1);
scrolled = el.scrollTop() > 0;
el.scrollTop(0);
if ( scrolled ) {
scrollable.push(this);
return;
}
});
if ( els === 'first' && scrollable.length ) {
scrollable = [ scrollable.shift() ];
}
return scrollable;
}
$.fn.extend({
scrollable: function() {
var scrl = getScrollable.call(this);
return this.pushStack(scrl);
},
firstScrollable: function() {
var scrl = getScrollable.call(this, 'first');
return this.pushStack(scrl);
},
smoothScroll: function(options) {
options = options || {};
var opts = $.extend({}, $.fn.smoothScroll.defaults, options);
this.die('click.smoothscroll').live('click.smoothscroll', function(event) {
var link = this, $link = $(this),
hostMatch = ((location.hostname === link.hostname) || !link.hostname),
pathMatch = opts.scrollTarget || (filterPath(link.pathname) || locationPath) === locationPath,
thisHash = link.hash,
include = true;
if ( !opts.scrollTarget && (!hostMatch || !pathMatch || !thisHash) ) {
include = false;
} else {
var exclude = opts.exclude, elCounter = 0, el = exclude.length;
while (include && elCounter < el) {
if ($link.is(exclude[elCounter++])) {
include = false;
}
}
var excludeWithin = opts.excludeWithin, ewlCounter = 0, ewl = excludeWithin.length;
while ( include && ewlCounter < ewl ) {
if ($link.closest(excludeWithin[ewlCounter++]).length) {
include = false;
}
}
}
if ( include ) {
opts.scrollTarget = options.scrollTarget || thisHash;
opts.link = link;
event.preventDefault();
$.smoothScroll(opts);
}
});
return this;
}
});
$.smoothScroll = function(options, px) {
var opts, $scroller, scrollTargetOffset,
scrollerOffset = 0,
offPos = 'offset',
dirs = {top: 'Top', 'left': 'Left'},
aniprops = {};
if ( typeof options === 'number') {
opts = $.fn.smoothScroll.defaults;
scrollTargetOffset = options;
} else {
opts = $.extend({}, $.fn.smoothScroll.defaults, options || {});
if (opts.scrollElement) {
offPos = 'position';
if (opts.scrollElement.css('position') == 'static') {
opts.scrollElement.css('position', 'relative');
}
}
scrollTargetOffset = px ||
( $(opts.scrollTarget)[offPos]() &&
$(opts.scrollTarget)[offPos]()[opts.direction] ) ||
0;
}
opts = $.extend({link: null}, opts);
if ( opts.scrollElement ) {
$scroller = opts.scrollElement;
scrollerOffset = $scroller.scrollTop();
} else {
$scroller = $('html, body').firstScrollable();
}
aniprops['scroll' + dirs[opts.direction]] = scrollTargetOffset + scrollerOffset + opts.offset;
$scroller.animate(aniprops,
{
duration: opts.speed,
easing: opts.easing,
complete: function() {
if ( opts.afterScroll && $.isFunction(opts.afterScroll) ) {
opts.afterScroll.call(opts.link, opts);
}
}
});
};
$.smoothScroll.version = version;
// default options
$.fn.smoothScroll.defaults = {
exclude: [],
excludeWithin:[],
offset: 0,
direction: 'top', // one of 'top' or 'left'
scrollElement: null, // jQuery set of elements you wish to scroll.
//if null (default), $('html, body').firstScrollable() is used.
scrollTarget: null, // only use if you want to override default behavior
afterScroll: null, // function to be called after window is scrolled. "this" is the triggering element
easing: 'swing',
speed: 400
};
// private function
function filterPath(string) {
return string
.replace(/^\//,'')
.replace(/(index|default).[a-zA-Z]{3,4}$/,'')
.replace(/\/$/,'');
}
})(jQuery);