views:

35

answers:

1

Ever notice that, when you're scrolling an overflowed div with a mousewheel, and you scroll that to the bottom, the whole page starts scrolling?

Can that be prevented?

I did a quick test of jQuery's scroll() event handler but it seems to fire way too late.

Here's the test code if you want to play around.

<html>
<head>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js" type="text/javascript" charset="utf-8"></script>
  <script type="text/javascript" charset="utf-8">
  $(function() {
    $('#scroller').scroll(function() {
      $('#notification').css('display','block').fadeOut();
      return false;
    })
  })
  </script>
</head>
<body style="height: 2000px">
  <div id="scroller" style="width: 500px; height: 500px; overflow: scroll; margin: 50px">
    <div style="width: 1000px; height: 1000px; background: #ddd"></div>
  </div>
  <div id="notification" style="display:none">Bang</div>
</body>
</html>
+1  A: 

This JS will do it, basically just set the body overflow to hidden when the mouse is over the div#scroller then set it back to normal when you mouse out.

$("#scroller").hover(
  function () {
        $('body').css('overflow','hidden');
  }, 
  function () {
        $('body').css('overflow','auto');
  }
);

Tested on Safari, Firefox and IE8

swaterfall
terrific! thanks.
lawrence