Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda