-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsmoothScroll.js
83 lines (65 loc) · 2.24 KB
/
smoothScroll.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
var smooth_scroll = (function() {
var intervalId;
var header_height;
var delta;
var destinationY;
var currentY;
var ignore_links;
var isInArray = function(value, array) {
return array.indexOf(value) > -1;
}
var smoothScroll = function() {
if (delta > 0 && currentY + delta > destinationY) {
delta = destinationY - currentY;
clearInterval(intervalId);
}
if (delta < 0 && currentY + delta < destinationY) {
delta = destinationY - currentY;
clearInterval(intervalId);
}
window.scrollBy(0, delta);
currentY = currentY + delta;
delta = delta * 1.1;
};
var scrollTo = function(el) {
if (intervalId !== undefined || intervalId !== null) {
clearInterval(intervalId);
}
destinationY = el.offsetTop - header_height;
currentY = window.scrollY;
intervalId = setInterval(smoothScroll.bind(el), 10);
delta = 1;
if (currentY > destinationY) {
delta = delta * -1;
}
};
var onClickAnchor = function(e) {
e.preventDefault();
var anchor_tag = this;
anchor_href = this.getAttribute("href")
element_id = anchor_href.slice(1, anchor_href.length);
var el = document.getElementById(element_id);
scrollTo(el);
};
var init = function(config) {
//Get all internal links
if (config !== undefined && config !== null) {
if (config.header_id !== undefined && config.header_id !== null) {
header_height = document.getElementById(config.header_id).getBoundingClientRect().height;
}
ignore_links = config.ignore_links || []
}
var internal_links = document.querySelectorAll("a[href^='#']");
for (var i = 0; i < internal_links.length; i++) {
link = internal_links[i];
var href = link.getAttribute("href");
if (href !== "#" && !isInArray(href.slice(1, href.length), ignore_links)) {
link.addEventListener("click", onClickAnchor.bind(link));
}
}
header_height = header_height || 0;
};
return {
init: init
}
})();