-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
38 lines (33 loc) · 1.4 KB
/
script.js
File metadata and controls
38 lines (33 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
document.addEventListener("DOMContentLoaded", function() {
const sidebarLinks = document.querySelectorAll("#sidebar a");
const sidebarHeader = document.querySelector("#sidebar .sidebar-header");
sidebarLinks.forEach(link => {
link.addEventListener("click", function(event) {
event.preventDefault();
const targetClass = this.getAttribute("href").substring(1); // Remove the '#' from href
highlightAndScrollTo(targetClass);
});
});
// Event listener for scrolling to top on sidebar header click
sidebarHeader.addEventListener("click", function(event) {
event.preventDefault();
window.scrollTo({
top: 0,
behavior: "smooth"
});
});
function highlightAndScrollTo(targetClass) {
// Remove highlight from all paragraphs
const allParagraphs = document.querySelectorAll("p");
allParagraphs.forEach(paragraph => {
paragraph.classList.remove("highlighted");
});
// Highlight the targeted paragraph
const targetParagraph = document.querySelector("." + targetClass);
if (targetParagraph) {
targetParagraph.classList.add("highlighted");
// Scroll to the targeted paragraph
targetParagraph.scrollIntoView({ behavior: "smooth", block: "start" });
}
}
});