-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.scrollto.js
51 lines (46 loc) · 1.45 KB
/
jquery.scrollto.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
/*!
* @author Thomas <thansen@solire.fr>
* @licence CC BY-NC 4.0 http://creativecommons.org/licenses/by-nc/4.0/
*/
(function($){
$.fn.scrollto = function(givenParams){
var defaultParams = {
speed : function(dst){
return dst;
},
indent : 0,
after : function(){}
},
isWebkit = /webkit/.test(navigator.userAgent.toLowerCase()),
container,
params = $.extend({}, defaultParams, givenParams),
base = this,
// where = $(base).offset().top + params.indent,
distance,
speed,
scrollTop;
if ('container' in params) {
container = params.container;
scrollTop = $(container).scrollTop() + $(base).position().top + params.indent;
} else {
container = isWebkit ? 'body' : 'html';
scrollTop = $(base).offset().top + params.indent;
}
distance = Math.abs($(container).scrollTop() - scrollTop);
if ($.isFunction(params.speed)) {
speed = params.speed.call(this, distance);
} else {
speed = params.speed;
}
$(container).animate(
{
scrollTop : scrollTop
},
speed,
function(){
params.after.call(this, base);
}
);
return base;
};
})(jQuery);