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

223
Visualizações
How to stop our document to be scroll, once a perticular section of of our document comes inside our view port in React.js?

What I am trying to achieve:

I Have three sections on my webpage first page, second page, and third page (those are section names, all three have 100vh and 100vw so they are cover our entire screen) and I want that once the second page comes in the viewport so the document should not be scrollable any more (i mean once our second page comes in viewport we can't scroll anymore to the third page it should stop scrolling)

what I tried in codepen. https://codepen.io/Adarsh04/pen/yLvppBK

rootElement

<div id="root"></div>

CSS

  body{
  -ms-overflow-style: none;
  scrollbar-width: none;
}

*::-webkit-scrollbar {
  /* width: 4px;
  cursor: pointer; */
  display: none;
}

.firstPage,.secondPage,.thirdPage{
  border:1px solid red;
  width:95vw;
  margin:atuo;
  height:95vh;
  display:flex;
  align-items:center;
  justify-content:center;
  font-size:64px;
}

.firstPage{
  background-color:red;
}

.secondPage{
  background-color:blue;
}

.thirdPage{
  background-color:yellow;
}

App.js

    const App = () => {
  React.useEffect(()=>{
    const handleEvent = () => {
      const bounding = document
        .getElementById("secondPage")
        .getBoundingClientRect();
      if (
        bounding.top >= 0 &&
        bounding.left >= 0 &&
        bounding.right <=
          (window.innerWidth || document.documentElement.clientWidth) &&
        bounding.bottom <=
          (window.innerHeight || document.documentElement.clientHeight)
      ) {
        console.log("Element is in the viewport!");
        document.body.style.overflow = "hidden";
      } else {
        console.log("Element is NOT in the viewport!");
      }
    };
    window.addEventListener("scroll", handleEvent, { passive: true });

    return () => {
      window.removeEventListener("scroll", handleEvent), { passive: true };
    };
  },[])
  return(
    <div className="box">
      <div className="firstPage">First.</div>
        
    <div id="secondPage" className="secondPage">Second.</div>
      
      <div className="thirdPage">Third</div>
    </div>
  );
}

ReactDOM.render(<App />,
document.getElementById("root"))

here the problem is only that when I scroll fastly that the second page is not stopped but if I scroll slowly its works and I want it should work regardless of how fast or slow I scroll

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