-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (53 loc) · 1.82 KB
/
index.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
const navToggle = document.querySelector('.nav-toggle');
const linksContainer = document.querySelector('.links-container');
const links = document.querySelector('.links');
const navbar = document.getElementById('nav-bar');
window.addEventListener('scroll', function () {
const scrollHeight = window.pageYOffset;
const navHeight = navbar.getBoundingClientRect().height;
if (scrollHeight > navHeight) {
navbar.classList.add('fixed-nav');
} else {
navbar.classList.remove('fixed-nav');
}
});
navToggle.addEventListener('click', function () {
// linksContainer.classList.toggle('show-links');
const linksHeight = links.getBoundingClientRect().height;
const containerHeight = linksContainer.getBoundingClientRect().height;
if (containerHeight === 0) {
linksContainer.style.height = `${linksHeight}px`;
} else {
linksContainer.style.height = 0;
}
console.log(linksContainer.getBoundingClientRect());
});
links.addEventListener('click', () => {
linksContainer.style.height = 0;
});
const scrollLinks = document.querySelectorAll('.nav-link');
scrollLinks.forEach((link) => {
link.addEventListener('click', (e) => {
// prevent default
e.preventDefault();
// navigate to specific spot
const id = e.currentTarget.getAttribute('href').slice(1);
const element = document.getElementById(id);
const navHeight = navbar.getBoundingClientRect().height;
const containerHeight = linksContainer.getBoundingClientRect().height;
const fixedNav = navbar.classList.contains('fixed-nav');
let position = element.offsetTop - navHeight;
if (!fixedNav) {
position = position - navHeight;
}
if (navHeight > 82) {
position = position + containerHeight;
}
window.scrollTo({
left: 0,
top: position,
});
// close
linksContainer.style.height = 0;
});
});