js开启和禁止页面滑动

首先,建立一个函数

function bodyScroll(event){  
    event.preventDefault();  
} 

之后在触发弹窗的时候禁止页面滚动

document.body.addEventListener('touchmove',bodyScroll,false);  
$('body').css({'position':'fixed',"width":"100%"});

关闭弹框时开启页面滚动

document.body.removeEventListener('touchmove',bodyScroll,false);   
$("body").css({"position":"initial","height":"auto"}); 

注意:切不可以下写法

document.body.addEventListener('touchmove', function (event) {  
    event.preventDefault();  
},false);  
document.body.removeEventListener('touchmove', function (event) {  
    event.preventDefault();  
},false); 

猜你喜欢

转载自blog.csdn.net/guohao326/article/details/83184424