Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda