How to disable scrolling temporarily?

前端 未结 30 3516
萌比男神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条回答
  •  深忆病人
    2020-11-21 05:22

    A simple solution that worked for me (disabling window scrolling temporarily).

    Based on this fiddle: http://jsfiddle.net/dh834zgw/1/

    the following snippet (using jquery) will disable the window scroll:

     var curScrollTop = $(window).scrollTop();
     $('html').toggleClass('noscroll').css('top', '-' + curScrollTop + 'px');
    

    And in your css:

    html.noscroll{
        position: fixed;
        width: 100%;
        top:0;
        left: 0;
        height: 100%;
        overflow-y: scroll !important;
        z-index: 10;
     }
    

    Now when you remove the modal, don't forget to remove the noscroll class on the html tag:

    $('html').toggleClass('noscroll');
    

提交回复
热议问题