vue项目中禁止页面滚动 / 滚动事件穿透 (弹出蒙版时,弹出层下面还可以滚动)
vue项目中弹出层时,蒙版下还可以滚动页面。
移动端解决方案
在蒙层所在div上加
@touchmove.prevent
<div class="maskBox" @touchmove.prevent></div>
PC端解决方案
弹层显示时
调用 stopMove()停止页面滚动
,弹层消失时
调用 Move()开启页面滚动
//停止页面滚动
stopMove(){
let m = function(e){e.preventDefault();};
document.body.style.overflow='hidden';
document.addEventListener("touchmove",m,{ passive:false });//禁止页面滑动
},
//开启页面滚动
Move(){
let m =function(e){e.preventDefault();};
document.body.style.overflow='';//出现滚动条
document.removeEventListener("touchmove",m,{ passive:true });
}