How to disable scrolling temporarily?

前端 未结 30 2609
萌比男神i
萌比男神i 2020-11-21 05:16

I\'m using the scrollTo jQuery plugin and would like to know if it is somehow possible to temporarily disable scrolling on the window element through Javascript? The reason

30条回答
  •  旧时难觅i
    2020-11-21 05:18

    I was looking out for a solution to this problem but was not satisfied with the any of the above solutions (as of writing this answer), so I came up with this solution..

    CSS

    .scrollDisabled {   
        position: fixed;
        margin-top: 0;// override by JS to use acc to curr $(window).scrollTop()
        width: 100%;
    }
    

    JS

    var y_offsetWhenScrollDisabled=0;
    
    function disableScrollOnBody(){
        y_offsetWhenScrollDisabled= $(window).scrollTop();
        $('body').addClass('scrollDisabled').css('margin-top', -y_offsetWhenScrollDisabled);
    }
    function enableScrollOnBody(){
        $('body').removeClass('scrollDisabled').css('margin-top', 0);
        $(window).scrollTop(y_offsetWhenScrollDisabled);
    }
    

提交回复
热议问题