Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

148
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!