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

150
Visualizações
How to detect scroll direction on a specific div

I found a very good solution from stack overflow about how to detect scroll direction and do something but this is working on an another div. I have a div with ID article-container that has scrollbar. I want to detect the scroll direction on this div not on the body or bigger parent div.

There is an onscroll event which has a function and I am trying to do everything inside it but is it causing any problem ...?

HTML

<div id="article-container" onscroll="scrollmainup()"> ... text ... </div>

JAVASCRIPT

function scrollmainup(){
var art = document.getElementById("article-container");
document.getElementById("article-container").scrollIntoView({
    block: 'center',
    behavior: 'smooth'
});
this.prev = window.scrollY;
art.addEventListener('scroll', e => this.handleNavigation(e));
handleNavigation = (e) => 
{
const window = e.currentTarget;
if (this.prev > window.scrollY) 
{
    console.log("scrolling up");
} 
else if (this.prev < window.scrollY) 
{
    console.log("scrolling down");
}
this.prev = window.scrollY;
};}

I've set art.addEventListener instead of window.addEventListener ... the variable art is not working ... something wrong with const window = e.currentTarget; not sure ... How can I make this code work on the article-container scrollable div ... the code works well on the whole page but not when I scroll the div ... please help me to set the eventlistener correctly ... Thanks in advance ...

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