-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubnav.js
More file actions
80 lines (70 loc) · 2.29 KB
/
Copy pathsubnav.js
File metadata and controls
80 lines (70 loc) · 2.29 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
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
75
76
77
78
79
80
window.addEventListener("DOMContentLoaded", () => {
initSectionNav("section2")
})
function initSectionNav(sectionId) {
let section = document.getElementById(sectionId)
let nav = section.querySelector(".subnav")
let links = [...nav.querySelectorAll("a")]
let subSections = links.map(link => section.querySelector(
link.getAttribute("href"))
)
links.forEach((link, index) => {
link.addEventListener("click", (event) => {
event.preventDefault()
let subsectionY = subSections[index].getBoundingClientRect().y
let navHeight = nav.getBoundingClientRect().height
window.scrollTo({
behavior: "smooth",
top: window.scrollY + subsectionY - navHeight
})
})
})
// OPTION 1 -- via scroll event
// window.addEventListener("scroll", () => {
// let active = -1
// active = subSections.findIndex((subSection, index) => {
// let elY = subSection.getBoundingClientRect().y
// let elHeight = subSection.getBoundingClientRect().height
// let navHeight = nav.getBoundingClientRect().height
//
// if (elY <= navHeight && elY + elHeight > navHeight) {
// return true
// }
// })
//
// links.forEach(link => {
// link.classList.remove("active")
// })
// if (active >= 0) {
// links[active].classList.add("active")
// }
// })
// OPTION 2 -- via intersection observer
let navHeight = nav.getBoundingClientRect().height
let screenHeight = document.documentElement.clientHeight
let buffer = 4
let options = {
rootMargin: `
-${navHeight + buffer}px
0px
-${screenHeight - navHeight - buffer}px
0px`,
}
let currentId = null
let observer = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (currentId != entry.target.id && entry.isIntersecting) {
currentId = entry.target.id
section.querySelector("a.active")?.classList.remove("active")
section.querySelector(`a[href="#${currentId}"]`)
.classList.add("active")
} else if (currentId == entry.target.id && !entry.isIntersecting) {
section.querySelector("a.active").classList.remove("active")
currentId = null
}
})
}, options
)
subSections.forEach((s) => {observer.observe(s)})
}