-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (42 loc) · 1.4 KB
/
Copy pathscript.js
File metadata and controls
48 lines (42 loc) · 1.4 KB
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
/*Sticky Navbar*/
const navButtons = document.querySelectorAll("nav>div")
const navbar = document.querySelector("nav")
const sectionTitles = document.querySelectorAll("section.title")
const sectionsArray = Array.from(sectionTitles)
const header = document.querySelector("header")
const startPosition = navbar.offsetTop
const observer = new IntersectionObserver(entries => adjustVisibility(entries))
function init() {
observer.observe(header)
for (let title of sectionTitles) {
observer.observe(title)
}
}
function adjustVisibility(entries) {
const mobileview = window.matchMedia("(max-width: 550px)")
for(let entry of entries){
if (!mobileview.matches) {
if (entry.target.nodeName === "HEADER") {
if (entry.intersectionRatio > 0) {
removeSelected()
navbar.classList.remove("sticky")
} else {
navbar.classList.add("sticky")
}
}
if (entry.target.nodeName === "SECTION") {
let i = sectionsArray.indexOf(entry.target)
if (entry.intersectionRatio > 0) {
removeSelected()
navButtons[i].classList.add("selected")
}
}
}
}
}
function removeSelected() {
for (let btn of navButtons) {
btn.classList.remove("selected")
}
}
init()