問題描述
在顯示彈窗時,如果底部頁面可滾動,可能在彈窗顯示時,滾動鼠標,會發生滾動穿透,讓底部頁面也一起滾動了。這種用戶體驗是不太好的,怎麼能阻止滾動穿透呢?
解決思路
讓彈窗顯示時,阻止背景的滾動事件,彈窗關閉時,重新恢復背景的滾動
示例(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 = ''
}