Seeking assistance with changing the background color of a component based on user scrolling behavior. I have attempted to achieve this using the following code snippet:
const [mainColor, setMainColor] = useState('red')
const listenScrollEvent = () => {
window.scrollY < 10
? setMainColor('green')
: setMainColor('red')
}
useEffect(() => {
window.addEventListener('scroll', listenScrollEvent)
})
return (
<main className={cx(classes.content, { [classes.disableScroll]: show })}
style={{ backgroundColor: mainColor }}
>
{children}
</main>
Below is the relevant CSS class for reference:
content: {
backgroundColor: theme.colors.gray[0],
position: 'fixed',
top: params.headerHeight,
right: 0,
left: 0,
bottom: 0,
overflowY: 'auto',
[mq(theme.breakpoints.sm)]: {
right: 0,
left: params.sidebarCollapsedWidth
}
I am unable to determine why window.scrollY
does not seem to be functioning properly as intended. I even tried logging window.scrollY
in the console but received no output. Any guidance from an expert would be greatly appreciated. Thank you.
Looking forward to your help!