22

I am having an issue where setting overflow-x: hidden on the html and body elements is preventing the jquery scroll event from firing.

CSS:

html, body {
  overflow-x: hidden;
}

JS:

$(function(){
  $(window).on("scroll", function(e){
    console.log("scrolling");    
  });
});

http://jsfiddle.net/r7Fqq/6/

Try it for yourself: Comment out overflow-x: hidden and pop open your console. You should see "scrolling" logged as you scroll up and down the html box. Comment it back in and the scroll event is silent.

Does anyone know why this is happening? I'm aware that when you set overflow to hidden it disables scrolling, but it should only do it for the axis that you are setting (x only in this case). Thanks in advance for any help.

Terry
  • 63,248
  • 15
  • 96
  • 118
jlarusso
  • 221
  • 1
  • 2
  • 4
  • 2
    How about `$('body').on('scroll')`? – Chad Jul 15 '13 at 22:38
  • 3
    More specifically, your window isn't scrolling, your html/body are. If you remove the `height: 100%;` your window scrolling will work fine. – Chad Jul 15 '13 at 22:42
  • 1
    when you set overflow-x: hidden, the scrollbar you see is for the body. Shrink the height, and you'll see two scrollbars, one for body, one for window. So ... what Torr3ent said. http://jsfiddle.net/r7Fqq/7/ – Dom Day Jul 15 '13 at 22:50
  • Thanks, that fixed it just fine. Appreciate the help. – jlarusso Jul 16 '13 at 13:49
  • Looks like similar issue, please have a look: http://stackoverflow.com/a/37185706/2050359 – rtrukhin May 12 '16 at 13:22
  • removing the height: 100%; in html/body css rules solved my issue. – Dushan Oct 12 '21 at 05:07

6 Answers6

26

Had the same problem. Solution is to remove the overflow-x: hidden from the html element and leave it only on the body element and it should work.

MSwehli
  • 483
  • 4
  • 12
11

I have also found this to be the case. When we added:

body {
  overflow-x: hidden;
}

all window.addEventListener('scroll') events stopped triggering. I believe this it is because the scroll event is not moved to the document.body. When I change the eventListener to document.body.addEventListener('scroll'), things start working again. The interesting thing is that my event.bubbles boolean is false. From what I read the scroll event should bubble to the window.

Solution

change

window.addEventListener('scroll', () => console.log('MEOW'))

to

document.body.addEventListener('scroll', () => console.log('MEOW'))
Matt Goo
  • 1,118
  • 1
  • 11
  • 12
3
$(function() {
    $(window).bind('mousewheel', function(event, delta) {
        console.log("mousewheel");
        return false;
    });
});
fritsMaister
  • 532
  • 1
  • 4
  • 18
  • Thanks, just what I need! In my case it was mandatory to have overflow hidden (x and y) on the whole document but needed to capture the scroll/mousewheel event to show an alert. – Javier Pallarés Sep 06 '19 at 10:47
0

Set body {height: 100%;} in css. It would work then. You may want to apply overflow property on a div than to whole body and then change JS code accordingly. Setting a overflow property on body takes away its scrolling ability. Another solution can be using Jquery wheel event as described in this post- (https://stackoverflow.com/a/8378946/5348972).

Community
  • 1
  • 1
Abhishek Sinha
  • 5,095
  • 1
  • 15
  • 14
0

Just add min-height: 100% to the body tag. Everything would be back on track.

Ravgeet Dhillon
  • 532
  • 2
  • 6
  • 24
0

For coming next visitors. I struggled with this issue a bit and I found that when the event is triggered on the element with the overflow then it solve the issue. I thought what might causing the issue and I have a guess about that, because the overflow is creating a layer stack of it's own separated from the body then when you scroll on the element and only when you meet the top or the bottom of the element it's actually trigger the window scroll event on the body which is on scroll top 0 or bottom end. So if the scroll event was on the element it would work:

document.getElementById('el').addEventListener('scroll', ()=>{console.log('scrolling')})
Eran Or
  • 1,252
  • 15
  • 22