How to disable scrolling temporarily?

前端 未结 30 3518
萌比男神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:17

    Do it simply by adding a class to the body:

    .stop-scrolling {
      height: 100%;
      overflow: hidden;
    }
    

    Add the class then remove when you want to re-enable scrolling, tested in IE, FF, Safari and Chrome.

    $('body').addClass('stop-scrolling')
    

    For mobile devices, you'll need to handle the touchmove event:

    $('body').bind('touchmove', function(e){e.preventDefault()})
    

    And unbind to re-enable scrolling. Tested in iOS6 and Android 2.3.3

    $('body').unbind('touchmove')
    

提交回复
热议问题