-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscroll.js
74 lines (63 loc) · 2.25 KB
/
scroll.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
const navToggleBtn = document.querySelector('.nav-toggle');
const linksContainer = document.querySelector('.links-container');
const links = document.querySelector('.links');
const da_te = document.getElementById('date');
const d = new Date();
da_te.innerHTML = d.getFullYear();
navToggleBtn.addEventListener('click', function () {
// linksContainer.classList.toggle('show-links')
let linksHeight = links.getBoundingClientRect().height;
let containerHeight = linksContainer.getBoundingClientRect().height;
if (containerHeight === 0) {
linksContainer.style.height = `${linksHeight}px`;
} else {
linksContainer.style.height = 0;
}
});
const nav = document.getElementById('nav');
const logo = document.querySelector('.logo');
const topLink = document.querySelector('.top-link');
window.addEventListener('scroll', function () {
const navHeight = nav.getBoundingClientRect().height;
// console.log(navHeight);
const scrollHeight = window.pageYOffset;
// console.log(scrollHeight);
if (scrollHeight > navHeight) {
nav.classList.add('fixed-nav');
logo.style.color = 'black';
} else {
nav.classList.remove('fixed-nav');
logo.style.color = 'white';
}
/// top link
if (scrollHeight > 520) {
topLink.classList.add('show-top-link');
} else {
topLink.classList.remove('show-top-link');
}
});
const scrollLinks = document.querySelectorAll('.scroll-link');
scrollLinks.forEach((link) => {
link.addEventListener('click', function (e) {
e.preventDefault();
const getId = e.currentTarget.getAttribute('href').slice(1);
// console.log(getId);
const id = document.getElementById(getId);
const navHeight = nav.getBoundingClientRect().height;
const containerHeight = links.getBoundingClientRect().height;
const fixedNav = nav.classList.contains('fixed-nav');
let sectionPosition = id.offsetTop - navHeight;
// console.log(sectionPosition);
if (!fixedNav) {
sectionPosition = sectionPosition - navHeight;
}
if (containerHeight > 83) {
sectionPosition = sectionPosition + containerHeight;
}
window.scrollTo({
top: sectionPosition,
left: 0,
});
linksContainer.style.height = 0;
});
});