-
Notifications
You must be signed in to change notification settings - Fork 0
/
move.js
35 lines (31 loc) · 938 Bytes
/
move.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
function addHandlers() {
let elems = document.querySelectorAll(".toc-button");
elems.forEach((c) => {
let id = c.id;
let elem = document.getElementById(id);
if (!elem) {return;}
elem.addEventListener("click", () => {
let hyphen = id.lastIndexOf("-");
moveTo(id.slice(0, hyphen));
});
});
elems = document.querySelectorAll(".project");
elems.forEach((c) => {
let id = c.id;
let elem = document.getElementById(id);
if (!elem) {return;}
elem.addEventListener("click", () => {
let hyphen = id.indexOf("-");
moveTo(id);
});
});
}
function moveTo(id) {
let elem = document.getElementById(id);
if (!elem) {return;}
let content = document.body;
let offsetTop = elem.offsetTop;
content.scrollTop = offsetTop;
window.location.hash = id;
}
window.onload = addHandlers;