JSFiddle - React, Tailwind, and code Playground
HTML
<div class="sticky">
<a href="#sec1Header">Section 1</a> |
<a href="#sec2Header">Section 2</a> |
<a href="#sec3Header">Section 3</a> |
<a href="#sec4Header">Section 4</a> |
<a href="#sec5Header">Section 5</a> |
<a href="#sec6Header">Section 6</a> |
</div>
<div class="card">
<h2 id="sec1Header">Section 1</h2>
Some text some text Some text some text Some text some text Some text some text
Some text some text Some text some text Some text some text Some text some text Some text some text
</div>
<div class="card">
<h2 id="sec2Header">Section 2</h2>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu, lobortis in odio. Praesent convallis urna a lacus interdum ut hendrerit risus congue. Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris
</div>
<div class="card">
<h2 id="sec3Header">Section 3</h2>
Nunc sagittis dictum nisi, sed ullamcorper ipsum dignissim ac. In at libero sed nunc venenatis imperdiet sed ornare turpis. Donec vitae dui eget tellus gravida venenatis. Integer fringilla congue eros non fermentum. Sed dapibus pulvinar nibh tempor porta. Cras ac leo purus. Mauris quis diam velit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus imperdiet, nulla et dictum interdum, nisi lorem egestas odio, vitae scelerisque enim ligula venenatis dolor. Maecenas nisl est, ultrices nec congue eget, auctor vitae massa. Fusce luctus vestibulum augue ut aliquet. Mauris ante ligula, facilisis sed ornare eu
</div>
<div...
CSS
div.sticky {
position: sticky;
top: 0;
background-color: yellow;
padding: 5px;
font-size: 20px;
}
.card {
border: 1px solid gray;
margin-top: 20px;
}
.active {
border: 2px solid red;
}
/* Prevent A-anchor scrolling behind sticky header */
:target:before {
content:"";
display:block;
height:90px; /* fixed header height*/
margin:-90px 0 0; /* negative fixed header height */
}
JavaScript
function updateMenu(){
const menus = document.querySelectorAll('div.sticky a');
const sectionsAll = document.querySelectorAll('div.card');
// Attempt to find matching menu
const sections = [...sectionsAll].filter(section => {
const sectionH2 = section.querySelectorAll('h2');
const sectionHeader =
(sectionH2.length ? sectionH2[0] :
null);
if (!sectionHeader) {
return false; //exclude
} else {
return true;
}
});
let currentSection = null;
for (var i = 0; i < sections.length; i++) {
const rect = sections[i].getBoundingClientRect();
// Find current section; if not last in the list, stop immediately when found
if(rect.top >= 0) {
currentSection = sections[i];
if (i < sections.length - 1) {
break;
}
}
}
if(currentSection){
// Try to find a matching menu based on the A innerText
menus.forEach(menu => {
const menuInnerText = menu.innerText;
const currentSectionH2 = currentSection.querySelectorAll('h2');
const currentSectionHeader =
(currentSectionH2.length ? currentSectionH2[0] :
null);
if (currentSectionHeader && menuInnerText == currentSectionHeader.innerText) {
// Select menu
menu.classList.add('active');
} else {
// Clear menu
menu.classList.remove('active');
}
});
} else {
// Deselect all menus
menus.forEach(menu => {
menu.classList.remove('active');
});
}
}
window.addEventListener('scroll', updateMenu);
updateMenu(); // initial call