Reputation: 400
In single page website am using IntersectionObserver to highlight active section. Cant figure out how to improve the remove active class part so that when scrolled outside any section the active is removed. In example below active class does not removes when scrolled back to top - when first section is not in sight.
// remove 'active' class from any navItem that is not
// same as 'navItem' defined above
Object.values(navItems).forEach((item) => {
if (item != navItem) {
item.classList.remove('active');
}
function selectElementByClass(className) {
return document.querySelector(`.${className}`);
}
const sections = [
selectElementByClass('services'),
selectElementByClass('contact'),
];
const navItems = {
services: selectElementByClass('servicesNavItem'),
contact: selectElementByClass('contactNavItem'),
};
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: .5,
};
function observerCallback(entries, observer) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const navItem = navItems[entry.target.id];
// add 'active' class on the navItem
navItem.classList.add('active');
// remove 'active' class from any navItem that is not
// same as 'navItem' defined above
Object.values(navItems).forEach((item) => {
if (item != navItem) {
item.classList.remove('active');
}
});
}
});
}
const observer = new IntersectionObserver(observerCallback, observerOptions);
sections.forEach((sec) => observer.observe(sec));
.nav-dots {position:fixed;}
.active {background: pink;}
.services {background: lightblue;}
.contact {background: lightyellow;}
<div class="nav-dots">
<ul class="navMenu">
<li><a href="#services" class="servicesNavItem navItem"><span class="nav-dot"></span><span class="nav-label">Services</span></a></li>
<li><a href="#contact" class="contactNavItem navItem"><span class="nav-dot"></span><span class="nav-label">Contact</span></a></li>
</ul>
</div>
<main class="page-main">
<h1>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a</h1>
<section id="services" class="services page-section">
<h2>Services</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
<section id="contact" class="contact page-section">
<h2>Contact</h2>
<p>orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
</main>
Upvotes: 1
Views: 358
Reputation: 1024
This is because the "sections" is not out of the viewport yet, which means you cannot get it again on the list of entries until it completely out of the page.
I can suggest you to use section headers instead to reduce the size of the rectangle that should reside within the viewport as follows;
function selectElementByClass(className) {
return document.querySelector(`.${className}`);
}
const sections = [
selectElementByClass('services'),
selectElementByClass('contact'),
];
const navItems = {
services: selectElementByClass('servicesNavItem'),
contact: selectElementByClass('contactNavItem'),
};
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.5,
};
function observerCallback(entries, observer) {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const navItem = navItems[entry.target.id];
navItem.classList.add('active');
Object.values(navItems).forEach((item) => {
if (item != navItem) {
item.classList.remove('active');
}
});
} /*else {
// this part reveals the main reason...
const navItem = navItems[entry.target.id];
navItem.classList.remove('active');
}*/
});
}
const observer = new IntersectionObserver(observerCallback, observerOptions);
sections.forEach((sec) => observer.observe(sec));
.nav-dots {
position:fixed;
}
.active {
background: pink;
}
<div class="nav-dots">
<ul class="navMenu">
<li><a href="#services" class="servicesNavItem navItem"><span class="nav-dot"></span><span class="nav-label">Services</span></a></li>
<li><a href="#contact" class="contactNavItem navItem"><span class="nav-dot"></span><span class="nav-label">Contact</span></a></li>
</ul>
</div>
<main class="page-main">
<h1>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a when an unknown printer took a galley of type and scrambled it to make a Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a</h1>
<section class="page-section">
<h2 id="services" class="services">Services</h2>
<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
<section class="page-section">
<h2 id="contact" class="contact">Contact</h2>
<p>orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
</section>
</main>
Upvotes: 1