Javascript Scroll Handler not firing
Asked Answered
M

6

44

All I'm trying to do is to call a function when a DIV is scrolled.For simplicity sake Im not specifying anything else. Also I am only looking at DOM compliant browsers like Chrome, Safari (not IE).

MY problem is that the scroll handler never gets called. If I replace the scroll to click , it works when I click. Somehow the scroll is not working.

Please note: I cannot use jQuery :(

Here is my code:

HTML:

<div id="test">--long content--</div>

JS:

   function myFunc() {
        console.log('in myFunc');
    }
    var objTable = document.getElementById("test");

    objTable.addEventListener("scroll", function () {
        myFunc();
    }, false);

FIDDLE:

http://jsfiddle.net/yymg5/7/

Michaelmas answered 7/3, 2013 at 16:7 Comment(1)
H
51

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);
Haggadist answered 7/3, 2013 at 16:13 Comment(2)
Thanks, you're right. I added an overflow attribute to my DIV and added some content.Now it works even on the DIV object. Have updated the fiddle.Michaelmas
The anonymous function is unnecessary. You can just pass myFunc as a first order function: window.addEventListener('scroll', myFunc, false);Responsory
A
27

i had the similar issue in my case. This code is correct, already answered by Undefined

window.addEventListener("scroll", function () {
    myFunc(); 
}, false);

but it wasn't working because window scroll event wasn't firing, since the body was scrolling instead of documentElement.

I just removed height: 100%; style from my body tag and then scroll event started firing.

Abdication answered 9/8, 2018 at 6:44 Comment(4)
@ElliotSchep i was focusing on height: 100% property of body.Abdication
Okay. Maybe a comment would have been better though?Requital
Had the same issue, and the css height: 100%; was the problem. Thanks for the fix!Sexpartite
You legend. I had this same issue in my Nexjtjs app! Removed height: 100% in the __next div and it then worked. This was trolling me for a while. Thank you!Ezequiel
R
19

If the scroll event is not firing despite best efforts then you could instead try wheel event:

document.addEventListener('wheel', (event) => {console.log('i scrolled')});
Roughcast answered 8/12, 2022 at 22:25 Comment(0)
L
6

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!')
 });
Loren answered 26/11, 2019 at 12:44 Comment(0)
H
3

I fixed it by removing height: 100%; from html and body CSS selectors.

Hagai answered 4/3, 2021 at 11:6 Comment(0)
A
2

It happened because your div has height: 100% or height: 100vh. In that case, the scroll event will disable automatically if you gave height:

Remove that div height, Remove any height on its parent, child. Basically, that particular div should scroll not its parent or child.

then this should work.

const AppWrapper = document.getElementById('app-content');
AppWrapper.addEventListener('scroll', toggleVisibility);


toggleVisibility = () => {
   console.log('Do your thg');
};
Almira answered 11/2, 2021 at 6:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.