-
Notifications
You must be signed in to change notification settings - Fork 3
/
scripts.js
34 lines (30 loc) · 1.08 KB
/
scripts.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
// create section links next to each heading of certain type
function createAnchors() {
const headers = document.querySelectorAll('h3');
for (const header of headers) {
// find closest parent element with id
let element = header;
while (!element.id && element !== document.body)
element = element.parentElement;
const id = element.id;
if (!id) continue;
// create link object
const link = document.createElement('a');
link.classList.add('anchor');
link.innerHTML = '<i class="fas fa-link fa-sm"></i>';
link.href = '#' + id;
header.appendChild(link);
}
}
// glow section when user navigates to it
function onHashChange() {
const id = window.location.hash.replace('#', '');
const element = document.getElementById(id);
if (!element) return;
// start css glow animation
element.setAttribute('data-glow', 'true');
window.setTimeout(() => element.removeAttribute('data-glow'), 2000);
}
window.addEventListener('load', createAnchors);
window.addEventListener('load', onHashChange);
window.addEventListener('hashchange', onHashChange);