5

I've got several divs on my page that look like this:

<div style="height:200px; overflow:auto;">
    <div style="height:300px;">
        <!--Lots of text here-->
    </div>
</div>

When the user hovers over them and scrolls with the scroll wheel they scroll fine. The trouble is that, when the user reaches the bottom, the page itself starts scrolling instead.

Is there a way to stop this happening (using javascript if necessary), so that the focus stays on the div even when it has scrolled to the bottom and the page itself only scrolls when the user is not hovered over a div?

Urbycoz
  • 7,247
  • 20
  • 70
  • 108
  • This question has been asked before. Here's a [demo](http://jsbin.com/ixura3/3) and the answer can be found here: http://stackoverflow.com/questions/5802467/prevent-scrolling-of-parent-element Basically, you need to bind the mousewheel event and use Javascript to control the scrolling of the child elements. His plug-in also has some interesting demos: http://brandonaaron.net/code/mousewheel/demos – Alex W May 09 '13 at 13:27

1 Answers1

2

Check this demo: http://jsfiddle.net/zUGKW/1/

Code:

// bind the mousewheel event
$('.myDiv').bind('mousewheel', function(e) {
    // if scroll direction is down and there is nothing more to scroll
    if(e.originalEvent.wheelDelta < 0 && 
    ($(this).scrollTop() + $(this).height()) >= this.scrollHeight) return false;
});
techfoobar
  • 65,616
  • 14
  • 114
  • 135