-
Notifications
You must be signed in to change notification settings - Fork 2
/
scrollToAnchor.js
64 lines (59 loc) · 1.89 KB
/
scrollToAnchor.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
/*! ScrollToAnchor.js v1.1,0 | Paul Browne | 2015 | GNU 2.0 */
(function() {
var bod = document.getElementsByTagName("body")[0];
var FIREFOX = /Firefox/i.test(navigator.userAgent);
if (FIREFOX) {
bod = document.getElementsByTagName("html")[0];
}
function animate(opts) {
var start = new Date(),
id = setInterval(function() {
var timePassed = new Date() - start,
progress = timePassed / opts.duration,
delta = opts.delta(progress);
if (progress > 1) {
clearInterval(id);
}
window.addEventListener('click', function() {
clearInterval(id);
});
opts.step(delta);
}, 1);
}
function move(duration, too) {
animate({
duration: duration || 1200,
delta: quad,
step: function(quad) {
bod.scrollTop = bod.scrollTop + (too - bod.scrollTop) * quad;
}
});
}
function quad(progress) {
return Math.pow(progress, 2);
}
function boo(evt) {
var thehref = this.getAttribute('href').slice(1),
idofhref = document.getElementById(thehref),
disttotop = idofhref.offsetTop,
length = Math.abs(disttotop - bod.scrollTop),
timing;
if (Math.abs(length) < 500) {
timing = 1000;
}
if (Math.abs(length) > 2500) {
timing = 2500;
} else {
timing = length;
}
move(timing, disttotop);
evt.preventDefault();
}
var elem = document.getElementsByTagName("a");
for (var i = 0; i < elem.length; ++i) {
var href = elem[i].getAttribute('href');
if (href.indexOf("#") === 0) {
elem[i].addEventListener('click', boo);
}
}
})();