Created
July 22, 2020 23:04
-
-
Save pupato13/7621627c5f841445f34c4295483f80fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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