With jQuery, how can I prevent the page from scrol

2019-09-18 05:50发布

I have a slide out mobile menu that has overflow set to auto so it will allow the user to scroll if the menu is too long.

I want to the user to be able to reach the end of the menu without the page scrolling on them.

I've tried each of these:

$(window).scroll(function(e){
     e.preventDefault();
});

$(window).scroll(function(e){
     e.preventDefault();
     e.stopPropagation();
});

$(window).scroll(function(e){
     return false
});

$('body').scroll(function(e){
     e.preventDefault();
});

$('body').scroll(function(e){
     e.preventDefault();
     e.stopPropagation();
});

$('body').scroll(function(e){
     return false
});

of course it test that the menu is open first. None of these prevent the page from scrolling.

1条回答
三岁会撩人
2楼-- · 2019-09-18 06:34

I think this will do the trick see the example on the fiddle

$(".scroll").bind( 'mousewheel DOMMouseScroll', function ( e ) {
                    //Get the original Event
                    var e0 = e.originalEvent,
                    //Hold the movement of the scroll 
                    delta = e0.wheelDelta ;
                    //If it's negative add -30 for each step or 30 if is positive
                    this.scrollTop += ( delta < 0 ? 1 : -1 ) * 30;
                    //Apply the scroll only for the element with the 
                    //handler      
                    e.preventDefault();
                    //Prevent the normal event
                }); 

Fiddle

查看更多
登录 后发表回答