I just started to use ReactJS. I currently want to change my Navbar color when user scroll to different section. For example, my section one is red and section 2 is green then my section 3 is blue. I want to let the Nav know that once User enter/scroll to section 1 and it will change color to make the color red and for section 2 and section 3 as well.
Right now I used the window.scroll to achieve this function.
const changeBackground = () => {
if(window.scrollY >= 898 && window.scrollY <= 1997){
//section = '1'
setColor('red')
}
else if(window.scrollY >= 1998 && window.scrollY <= 3097) {
//section = '2'
setColor('green')
}
else if(window.scrollY >= 3098) {
//section = '3'
setColor('blue')
}
My Current problem is that my height is fixed so section 1 is between 898 and 1997px. However I want to set the height to 100vh so that each section fill up the screen, and based the the 100vh, the px will be different depends on the user screen size.
Any thoughts/tips to let the nav know which section it is on right now?
Thanks so much! happy coding.