Javascript Scroll Handler not firing
i had the similar issue in my case. This code is correct, already answered by Undefined
window.addEventListener("scroll", function () {
myFunc();
}, false);
but it was not working because,
scroll event wasn't firing. since my body was scrolling instead of documentElement.
I just removed height: 100%
from my body tag and then scroll event started firing.
This is because the window is scrolling not the div. Try changing your element listener to the parent of the div (in this case the window) like this.
window.addEventListener("scroll", function () {
myFunc();
}, false);
I fixed it by removing height: 100%;
form html
and body
CSS selectors.
In my case, I have a height: 100%
in my body. Since I just wanted to apply the scroll event in a special div only.
Then, this fixed the issue:
document.querySelector('#myDiv').addEventListener('scroll', () => {
console.log('scroll event fired!')
});