vue中遮罩层阻止鼠标滚动及页面滚动

6 篇文章 0 订阅
2 篇文章 0 订阅

vue提供一个简易的事件

@touchmove.prevent ——阻止页面滚动
@mousewheel.prevent——阻止鼠标滚动

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Vue3的项目,可以通过以下步骤去掉遮罩层下方的滚动条: 1. 在遮罩层的样式,设置`position: fixed`和`overflow-y: scroll`,例如: ```css .mask { position: fixed; top: 0; left: 0; bottom: 0; right: 0; background-color: rgba(0, 0, 0, 0.6); z-index: 999; overflow-y: scroll; } ``` 2. 在需要显示遮罩层的组件,使用Vue的`computed`属性计算出一个布尔值,用来判断是否需要显示遮罩层,例如: ```html <template> <div> <button @click="showMask = true">显示遮罩层</button> <div v-if="showMask" class="mask"></div> </div> </template> <script> export default { data() { return { showMask: false, }; }, computed: { bodyStyle() { return this.showMask ? { overflow: 'hidden' } : { overflow: 'auto' }; }, }, }; </script> <style> body { height: 100%; } </style> ``` 3. 在组件的`<body>`元素上绑定样式,根据是否显示遮罩层来决定是否隐藏滚动条,例如: ```html <template> <div> <button @click="showMask = true">显示遮罩层</button> <div v-if="showMask" class="mask"></div> </div> </template> <script> export default { data() { return { showMask: false, }; }, computed: { bodyStyle() { return this.showMask ? { overflow: 'hidden' } : { overflow: 'auto' }; }, }, mounted() { Object.assign(document.body.style, this.bodyStyle); }, updated() { Object.assign(document.body.style, this.bodyStyle); }, }; </script> <style> body { height: 100%; } </style> ``` 这样就可以在Vue3的项目去掉遮罩层下方的滚动条了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

这次最后一次熬夜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值