Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

170
Views
navbar scroll Y event

i currently have a navbar component and would like to change its background color on scroll. I am trying to detect the scroll height but it is not working. Every time i open the console, my console.log keeps returning 0 meaning that the scroll is not getting detected. I have looked at many question regarding this problem and tried applying their solutions but still it is not working.

Here is my code:

Navbar.js

function Navbar() {

    const[navbar,setNavbar]=useState(false);

    const changeBackground=()=>{
        console.log(window.scrollY)
            if(window.scrollY >= 80){
                setNavbar(true)
            } else{
                setNavbar(false)
            }
    }

    window.addEventListener('scroll',changeBackground,true);

    return (
        <>
            <div className={navbar ? 'navbar_container active':'navbar_container'}>
           </div>
         )
       }
export default Navbar;

Navbar.css

.navbar_container {
    align-items: center;
    background-color: transparent;
    color: white;
    display: flex;
    flex-direction: row;
    height: 94px;
    justify-content: space-between;
    margin: 0 auto;
    width: 98%;
    z-index: 100;
    position: fixed;
    width: 100vw;
}

.navbar_container.active {
    align-items: center;
    background-color: black;
    color: white;
    display: flex;
    flex-direction: row;
    height: 94px;
    justify-content: space-between;
    margin: 0 auto;
    width: 98%;
    z-index: 100;
    position: fixed;
    width: 100vw;
}

Home.js

function Home(){
return(
<Navbar/>
)}
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!