1

How to prevent scroll event?

I was trying to add event.preventDefault() into the HostListener but it's not working. event.stopPropagation() and event.stopImmediatePropagation() are not working too.

I need to implement some logic instead of scrolling.

@HostListener('window:scroll', ['$event'])
onScroll(event) {
  event.preventDefault();
  console.log('Hello World');
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Dmitry Grinko
  • 13,806
  • 14
  • 62
  • 86
  • try using `'wheel'` instead of `'window:scroll'` – RadekF Apr 09 '19 at 17:54
  • @RadekF Thank you! It's working. Add your answer – Dmitry Grinko Apr 09 '19 at 17:57
  • Please edit the question to make it clear that you want to intercept the scrolling with the mouse wheel (not using the scroll bar). – ConnorsFan Apr 09 '19 at 18:02
  • @ConnorsFan Actually I want to intercept scroll event. It doesn't matter wheel or scroll bar. If you know how to prevent both triggers let me know please. – Dmitry Grinko Apr 09 '19 at 18:05
  • 2
    You can use the trick given in [this answer](https://stackoverflow.com/a/4770059/1009922): reset the scroll position in the event handler. See [this stackblitz](https://stackblitz.com/edit/angular-bp8lq9?file=src%2Fapp%2Fapp.component.ts) for a demo. – ConnorsFan Apr 09 '19 at 18:22
  • 2
    And here is [another stackblitz](https://stackblitz.com/edit/angular-plyafa?file=src%2Fapp%2Fapp.component.ts) showing how to allow/disallow scrolling. – ConnorsFan Apr 09 '19 at 18:35

1 Answers1

2

Try using 'wheel' instead of 'window:scroll'

RadekF
  • 476
  • 5
  • 10