I would like to ask about scroll listener. I want to add scroll listener on body but it seems doesnt work.
$('body').scroll(function(){
console.log('SCROLL BODY');
});
I create basic example on fiddle, can someone explain me why it doesn't to work? Sorry for nubies question...
Because the body isn't scrolling, the
window
is.In This example, you'll see that the event listener bound to the parent
container
is what's firing, because that element is the one that's actually scrolling.The HTML looks like this:
The CSS looks like this:
And the relevant JS looks like this:
Try with:
This should be supported by all browsers.
All the answers above expect jQuery being the framework of use. A framework agnostic / plain JS implementation could look like this
ES 5:
ES 6 (and above) :