Skip to content

Instantly share code, notes, and snippets.

@pupato13
Created July 22, 2020 23:04
Show Gist options
  • Save pupato13/7621627c5f841445f34c4295483f80fc to your computer and use it in GitHub Desktop.
Save pupato13/7621627c5f841445f34c4295483f80fc to your computer and use it in GitHub Desktop.
useEffect(() => {
const handleScroll = () => {
const currentScrollY = window.scrollY;
// Handle Menu Transparency
if (
currentScrollY > 50 &&
headerBackgroundColor === "transparent"
) {
setTransparentHeader(false);
setHeaderBackgroundColor("#fff");
setLogo(bossLogo);
}
if (currentScrollY <= 50) {
setTransparentHeader(true);
setHeaderBackgroundColor("transparent");
setLogo(bossLogoGreen);
}
// Handle Section is visible
const sectionsLanding = document.querySelectorAll(
".landing-section",
);
let minorDistance = window.innerHeight;
let activeSection;
if (sectionsLanding) {
[].forEach.call(sectionsLanding, function (
sectionItem: Element,
) {
var offset = sectionItem.getBoundingClientRect();
if (Math.abs(offset.top) < minorDistance) {
minorDistance = Math.abs(offset.top);
activeSection = sectionItem;
}
});
if (activeSection) {
const sectionId = (activeSection as Element).id;
if (sectionId) {
const menuLink = menuLinks.find(
link => link.section === sectionId,
);
if (menuLink?.id === activeLink) return;
if (menuLink) {
setActiveLink(menuLink.id);
}
}
}
}
prevScrollY.current = currentScrollY;
};
window.addEventListener("scroll", handleScroll, { passive: true });
return () => window.removeEventListener("scroll", handleScroll);
}, [headerBackgroundColor]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment