SGN SGN - 1 year ago 314
TypeScript Question

Tracking scroll not working with Internet Explorer 11

I have the following code:

this.currentScrollYSub = Observable.fromEvent(window, 'scroll')
.subscribe(e => {
this.scrollY = window.scrollY;
console.log(window.scrollY); // Result: undefined

Which works just fine on Chrome, but I noticed that on IE 10 and IE 11, it doesn't work.

How can i make this work on Internet Explorer 11?


I also tried something like this:

@HostListener('window:scroll', ['$event'])
track(event) {
console.debug("Scroll Event ", document.body.scrollTop); // Result: "Scroll Event 0"

@HostListener('window:scroll', ['$event'])
track(event) {
console.debug("Scroll Event ", this.scrollY); // Result: "Scroll Event undefined"

Answer Source

Internet Explorer: No support

You can use something like:

return {
  x: window.pageXOffset || document.documentElement.scrollLeft,
  y: window.pageYOffset || document.documentElement.scrollTop

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download