-
Notifications
You must be signed in to change notification settings - Fork 34
/
even.js
81 lines (70 loc) · 2.48 KB
/
even.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
function initMobile() {
var $mobileNav = document.getElementById("mobile-navbar");
var $mobileNavIcon = document.querySelector(".mobile-navbar-icon");
var slideout = new Slideout({
"panel": document.getElementById("mobile-panel"),
"menu": document.getElementById("mobile-menu"),
"padding": 180,
"tolerance": 70
});
slideout.disableTouch();
$mobileNavIcon.addEventListener("click", function() {
slideout.toggle();
});
slideout.on("beforeopen", function () {
$mobileNav.classList.add("fixed-open");
$mobileNavIcon.classList.add("icon-click");
$mobileNavIcon.classList.remove("icon-out");
});
slideout.on("beforeclose", function () {
$mobileNav.classList.remove("fixed-open");
$mobileNavIcon.classList.add("icon-out");
$mobileNavIcon.classList.remove("icon-click");
});
document.getElementById("mobile-panel").addEventListener("touchend", function() {
slideout.isOpen() && $mobileNavIcon.click();
})
}
function initToc() {
var $toclink = document.querySelectorAll('.toc-link')
var $headerlink = document.querySelectorAll('.post-content h1 , .post-content h2')
var $tocLinkLis = document.querySelectorAll('.post-toc-content li')
var searchActiveTocIndex = function (array, target) {
if (!array.length) {
return -1
}
target += 30
for (let i = 0; i < array.length - 1; i++) {
if (target > array[i].offsetTop && target <= array[i + 1].offsetTop) return i
}
if (target > array[array.length - 1].offsetTop) return array.length - 1
return -1
}
document.addEventListener("scroll", function() {
var scrollTop = document.body.scrollTop | document.documentElement.scrollTop
var activeTocIndex = searchActiveTocIndex($headerlink, scrollTop)
$toclink.forEach(function (el) {
el.classList.remove('active')
})
$tocLinkLis.forEach(function (el) {
el.classList.remove('has-active')
})
if ($toclink.length && activeTocIndex !== -1) {
$toclink[activeTocIndex].classList.add('active')
let ancestor = $toclink[activeTocIndex].parentNode
while (ancestor.tagName !== 'NAV') {
ancestor.classList.add('has-active')
ancestor = ancestor.parentNode.parentNode
}
}
})
}
if (document.readyState === "complete" ||
(document.readyState !== "loading" && !document.documentElement.doScroll)
) {
initMobile();
initToc();
} else {
document.addEventListener("DOMContentLoaded", initMobile);
document.addEventListener("DOMContentLoaded", initToc);
}