How to disable scrolling temporarily?

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

    As of Chrome 56 and other modern browsers, you have to add passive:false to the addEventListener call to make preventDefault work. So I use this to stop scrolling on mobile:

    function preventDefault(e){
        e.preventDefault();
    }
    
    function disableScroll(){
        document.body.addEventListener('touchmove', preventDefault, { passive: false });
    }
    function enableScroll(){
        document.body.removeEventListener('touchmove', preventDefault, { passive: false });
    }
    

提交回复
热议问题