-
Notifications
You must be signed in to change notification settings - Fork 0
/
chapters_js.js
87 lines (77 loc) · 2.23 KB
/
chapters_js.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
function iconMenu() {
var x = document.getElementById("cimsor");
x.classList.toggle("open");
}
initSmoothScrolling();
function initSmoothScrolling() {
document.querySelectorAll('a')
.forEach(function(a) {
a.addEventListener('click', onClick, false);
});
function onClick(e) {
e.stopPropagation();
e.preventDefault();
jump(e.target.hash, {
duration: 500,
});
}
}
function jump(target, options) {
var
start = window.pageYOffset,
opt = {
duration: options.duration,
offset: options.offset || 0,
callback: options.callback,
easing: options.easing || easeInOutQuad
},
distance = typeof target === 'string' ?
opt.offset + document.querySelector(target).getBoundingClientRect().top :
target,
duration = typeof opt.duration === 'function' ?
opt.duration(distance) :
opt.duration,
timeStart, timeElapsed;
requestAnimationFrame(function(time) {
timeStart = time;
loop(time);
});
function loop(time) {
timeElapsed = time - timeStart;
window.scrollTo(0, opt.easing(timeElapsed, start, distance, duration));
if (timeElapsed < duration)
requestAnimationFrame(loop)
else
end();
}
function end() {
window.scrollTo(0, start + distance);
if (typeof opt.callback === 'function')
opt.callback();
}
function easeInOutQuad(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
}
}
(function() {
'use strict';
var section = document.querySelectorAll(".chapter");
var sections = {};
var i = 0;
window.onscroll = function() {
Array.prototype.forEach.call(section, function(e) {
sections[e.id] = e.offsetTop-250;
});
var scrollPosition = document.documentElement.scrollTop || document.body.scrollTop;
for (i in sections) {
if (sections[i] <= scrollPosition) {
document.querySelector('.active').classList.remove('active');
document.querySelector('a[href*=' + i + ']').className += ' active';
}
}
};
})();