I'm doing something with Scrollspy Vertical Menu. But I ran into a problem and couldn't find a solution. when I scroll, I want it to follow the nav on my sidebar slider. Because I can't see what part he's in when there's a lot of sections. how can I solve this? I tried bootstrap's scrollspy component, but it didn't work there, I wonder if anyone has a better solution?
https://codepen.io/lonelylonely/pen/mdxZBRM
const sections = document.querySelectorAll("section");
const navLinks = document.querySelectorAll("nav a");
const nav = document.querySelector("nav");
const lastElHeight =
sections[sections.length - 1].getBoundingClientRect().height + 300;
window.onscroll = (el) => {
sections.forEach((section) => {
let top = window.scrollY;
let navHeight = nav.offsetHeight;
let offset = section.offsetTop;
let height = section.offsetHeight;
let id = section.getAttribute("id");
if (top >= offset && top < offset + height) {
navLinks.forEach((links) => {
links.classList.remove("active");
document.querySelector(`nav a[href*="${id}"]`).classList.add("active");
});
}
if (
sections[sections.length - 1].getBoundingClientRect().top < lastElHeight
) {
navLinks.forEach((links) => {
links.classList.remove("active");
document.querySelector(`nav a[href*="${id}"]`).classList.add("active");
});
}
});
};
<nav style="height: 200px; overflow-x: scroll;">
<a href="#section-1" class="active">Section 1</a>
<a href="#section-2">Section 2</a>
<a href="#section-3">Section 3</a>
<a href="#section-4">Section 4</a>
<a href="#section-5">Section 5</a>
<a href="#section-6">Section 6</a>
<a href="#section-7">Section 7</a>
<a href="#section-8">Section 8</a>
<a href="#section-9">Section 9</a>
<a href="#section-10">Section 10</a>
</nav>
<div class="section-container" tabindex="0">
<section id="section-1">Section 1 <p>(200px height)</p>
</section>
<section id="section-2">Section 2 <p>(800px height)</p>
</section>
<section id="section-3">Section 3 <p>(400px height)</p>
</section>
<section id="section-4">Section 4 <p>(1200px height)</p>
</section>
<section id="section-5">Section 5 <p>(600px height)</p>
</section>
<section id="section-6">Section 6 <p>(600px height)</p>
</section>
<section id="section-7">Section 7 <p>(600px height)</p>
</section>
<section id="section-8">Section 8 <p>(600px height)</p>
</section>
<section id="section-9">Section 9 <p>(600px height)</p>
</section>
<section id="section-10">Section 10 <p>(600px height)</p>
</section>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
display: flex;
position: relative;
}
html {
scroll-behavior: smooth;
overflow-y: scroll;
height: 100vh;
}
nav {
display: flex;
position: fixed;
flex-direction: column;
top: 0;
width: 20%;
}
nav a {
padding: 10px 25px;
color: black;
font-size: 20px;
text-transform: capitalize;
background-color: lightgray;
font-family: sans-serif;
text-align: center;
text-decoration: none;
}
nav a.active,
nav a:hover {
background-color: darkgray;
}
section {
display: flex;
align-items: center;
justify-content: center;
font-weight: 800;
text-transform: uppercase;
font-size: 7rem;
font-family: sans-serif;
border: 1px solid gray;
flex-direction: column;
}
section p {
font-size: 1rem;
font-weight: 300;
}
section:nth-child(odd) {
background-color: #eee;
}
.section-container {
width: 80%;
margin-left: auto;
}
#section-1 {
height: 200px;
}
#section-2 {
height: 800px;
}
#section-3 {
height: 400px;
}
#section-4 {
height: 1200px;
}
#section-5 {
height: 600px;
}
#section-6 {
height: 600px;
}
#section-7 {
height: 600px;
}
#section-8 {
height: 600px;
}
#section-9 {
height: 600px;
}
#section-10 {
height: 600px;
}