问题描述
在显示弹窗时,如果底部页面可滚动,可能在弹窗显示时,滚动鼠标,会发生滚动穿透,让底部页面也一起滚动了。这种用户体验是不太好的,怎么能阻止滚动穿透呢?
解决思路
让弹窗显示时,阻止背景的滚动事件,弹窗关闭时,重新恢复背景的滚动
示例(vue)
<div>
原背景页面
</div>
<div v-if="showDialog" class="dialog" ref="dialog">
<div class="dialog-content">
此处省略一万字......
</div>
</div>
methods: {
showList (i) {
this.showDialog = true
document.querySelector('body').style.overflow = 'hidden'
},
closeDialog () {
this.showDialog = false
document.querySelector('body').style.overflow = ''
},
}
mounted() {
this.fn = (e) => {
e.preventDefault()
}
this.$refs.healthplan.querySelector('.dialog').addEventListener('touchmove', this.fn)
},
beforeDestroy() {
this.$refs.healthplan.querySelector('.dialog ').removeEventListener('touchmove', this.fn)
document.querySelector('body').style.overflow = ''
}