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

119
Visualizações
Changing color of Navbar react js

I am trying to change the color of my navbar using this code.

  const [navbarColor, setNavbarColor] = useState("navbar-transparent");
  const [collapseOpen, setCollapseOpen] = useState(false);
  useEffect(() => {
    const updateNavbarColor = () => {
      if (
        document.documentElement.scrollTop > 399 ||
        document.body.scrollTop > 399
      ) {
         setNavbarColor("");
      } else if (
        document.documentElement.scrollTop < 400 ||
        document.body.scrollTop < 400
      ) {
        setNavbarColor("navbar-transparent");
      }
    };
    window.addEventListener("scroll", updateNavbarColor);
    return function cleanup() {
      window.removeEventListener("scroll", updateNavbarColor);
    };
  });

This is how I am making navbar

      <Navbar className={"fixed-top " + navbarColor} expand="lg" color="info">

However, when I scroll down the web page crashes, although the code looks fine.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try this logic code.

import { useEffect } from 'react'


export function useObservableAnimations() {

  const animationOnScroll = () => {
 const target = document.querySelectorAll('[data-animation')
    target.forEach(element => {
      if (element.getBoundingClientRect().top < window.innerHeight) {
        element.classList.add('animation-start')
        return
      }
      element.classList.remove('animation-start')
    })
  }
  useEffect(() => {
    window.addEventListener('scroll', animationOnScroll)
  }, [])
}
about 4 years ago · Juan Pablo Isaza Relatório
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