Day # 29: Sticky nav scroll
Sticky nav bars are great features for the look of your website. They add an appealing amount of dynamism to them, making them more elegant and sophisticated.
I recently ran into an article written by Ryan Mulligan, in which he actually proposes a way to change the background color of a sticky nav bar depending on where you scroll.
I thought this adds an extra layer of sophistication to our websites, so I decided to follow his instructions. In his article, Ryan mentions the Interception Observer API, which “is being used to observe when the intercept is no longer appearing in the visible viewport area which happens as soon as the page scrolls.”
We just need to find a way to make that interception to change the style of the nav bar.To achieve that, we just need to add an attribute to that element we are trying to intercept so that we can later handle it through Javascript:
<div data-observer-intercept></div>
<header id="page-header">
//...
</header>
const observer = new IntersectionObserver(([entry]) => {
header.classList.toggle("active", !entry.isIntersecting);
});
observer.observe(intercept);
Comments
Post a Comment