Prevent parent scroll when in child div

后端 未结 5 1642
野的像风
野的像风 2021-02-08 12:08

When I scroll to the bottom of the child div, the body element starts scrolling.

How can I prevent this? I only want the body to s

5条回答
  •  广开言路
    2021-02-08 12:36

    Accepted answer seems outdated. The "mousewheel" event is a non-standard feature. The "wheel" event seems to be the standard now. Also wheelDelta isn't defined in most browsers. Change to -event.deltaY seems to do the trick. Works in IE 10, Chrome and Firefox

    $(".scroll-div").on("wheel", function ( e ) {
                var event = e.originalEvent,
                    d = -event.deltaY || -event.detail ;
    
                this.scrollTop += ( d < 0 ? 1 : -1 ) * 30;
                e.preventDefault();
        });
    

提交回复
热议问题