首页 > 其他分享 >vue 实现遮罩层禁止滚动事件

vue 实现遮罩层禁止滚动事件

时间:2022-12-31 11:32:32浏览次数:38  
标签:遮罩 style vue 滚动 touchmove document 页面


我们在写弹出的遮罩层时,下方的页面依然可以滚动,这时我们就要想些办法来实现了。

移动端

vue中提供 @touchmove.prevent 方法可以完美解决这个问题

在蒙层所在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 });
}


标签:遮罩,style,vue,滚动,touchmove,document,页面
From: https://blog.51cto.com/u_14785218/5982063

相关文章