5

How can I detect horizontally scrolling on an element

I have a div in a containing div that scrolls left to right.

I want to detect the when the div is scrolling.

I can use this to detect the window scrolling, but how do I detect one element scrolling

@HostListener("window:scroll", []) onWindowScroll() {
    console.log('scrolling');
    const verticalOffset = window.pageYOffset
        || document.documentElement.scrollTop
        || document.body.scrollTop || 0;
}
1
  • Build an Angular directive with @HostListener and attach it to the element you wan't to detect scrolling on. Commented Apr 30, 2018 at 14:08

1 Answer 1

12

You can handle the scroll event of the container div:

<div class="container" (scroll)="onScroll($event)">
  <div class="content">
    Some content 
  </div>
</div>
onScroll(event: Event) {
  console.log(event);
  ...
}

See this stackblitz for a demo.

Sign up to request clarification or add additional context in comments.

1 Comment

this is not really realiable solution.. In my huge list on IE is a big problem while continues scroll UP, DOWN and LEFT, RIGHT. Looking for solution to detect only horizontal scroll

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.