-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
26 lines (23 loc) · 776 Bytes
/
index.js
File metadata and controls
26 lines (23 loc) · 776 Bytes
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
/**
* Scrolls the browser window to the element with the given
* id attribute, setting its relative location to be on top.
*/
function scrollToElement() {
// Find and remove the last "active" nav element, if exists
let lastActive = getLastActive();
if (lastActive) {
lastActive.classList.remove('active');
}
window.event.target.classList.add('active');
let id = window.event.target.getAttribute('data-target')
let el = document.getElementById(id);
let nav = document.querySelector('nav');
let top = el.getBoundingClientRect().top + window.pageYOffset - nav.offsetHeight - 4;
window.scrollTo({
top: top,
behavior: 'smooth'
});
}
function getLastActive() {
return document.querySelector('.active');
}