forked from softlayer/softlayer-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscrollable.js
More file actions
59 lines (50 loc) · 1.65 KB
/
scrollable.js
File metadata and controls
59 lines (50 loc) · 1.65 KB
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
/*
# Scrollable
# Thresholds and smooth animations for page scrolling
#
# Copyright © SoftLayer, an IBM Company
# Code and documentation licensed under MIT
*/
pageOffset = document.documentElement.scrollTop || document.body.scrollTop;
scrollTo = function(element, to, duration) {
start = element.scrollTop;
change = to - start;
currentTime = 0;
increment = 20;
animateScroll = function() {
currentTime += increment;
val = Math.easeInOutQuad(currentTime, start, change, duration);
element.scrollTop = val;
if (currentTime < duration) {
setTimeout(animateScroll, increment);
}
};
Math.easeInOutQuad = function(t, b, c, d) { t /= d / 2;
if (t < 1) { return c / 2 * t * t + b; }
t--; return -c / 2 * (t * (t - 2) - 1) + b; };
animateScroll();
};
window.onscroll = function() {
if (pageYOffset >= 200) {
document.getElementById("top").style.visibility = "visible";
}
else {
document.getElementById("top").style.visibility = "hidden";
return;
}
document.getElementById("top").onclick = function() {
scrollTo(document.body, 0, 0);
};
};
$("a[href*=#]:not([href=#])").click(function() {
if (location.pathname.replace(/^\//,"") == this.pathname.replace(/^\//,"") || location.hostname == this.hostname) {
var target = $(this.hash);
target = target.length ? target : $("[name=" + this.hash.slice(1) +"]");
if (target.length) {
$("html,body").animate({
scrollTop: target.offset().top
}, 1000);
return false;
}
}
});