arkon / ng-sidebar

[Inactive] Angular sidebar component.
https://echeung.me/ng-sidebar
MIT License
388 stars 136 forks source link

window scroll retrieve scroll amount #200

Closed amarchino closed 4 years ago

amarchino commented 4 years ago

Greetings.

Referring to Issues #117 and #129 I added a directive to the ng-sidebar-content, read the the scroll event from its parent and tried to log the scrolling amount.

It all works, except that the scrolling amount is always 0: more precisely

console.log(`window.pageYOffset: ${window.pageYOffset}
window.scrollY: ${window.scrollY}
document.documentElement.scrollTop: ${document.documentElement.scrollTop}
document.body.scrollTop: ${document.body.scrollTop}`);

returns:

window.pageYOffset: 0
window.scrollY: 0
document.documentElement.scrollTop: 0
document.body.scrollTop: 0

The subscription is just (excerpted from a mostly "clean" project)

Observable.fromEvent(this.el.nativeElement.parentElement, 'scroll')
.subscribe(() => console.log(<...>))

Is anything wrong with the handling of the code? Should I not be able to retrieve the scrolling amount? Am I doing something wrong, or reading the wrong parameters to view the scroll?

Thanks

amarchino commented 4 years ago

Ehmmm... I spent the last three days trying to tackle this problem... And mere hours after posting I found a possible solution.

Apparently, nor the window object, nor the document nor the body will actually scroll: the scrolling element is still this.el.nativeElement.parentElement. Therefore my logging should have been written as

console.log(`this.el.nativeElement.parentElement.scrollTop: ${this.el.nativeElement.parentElement.scrollTop}`);

This issue may therefore be closed... But may I suggest @arkon to write some indications about this behavior (both the element to listen to while scrolling, and the element whose scroll value should be looked up) in the documentation, so as to help future users of Your library?

Thanks!