Vue項目移動端滾動穿透問題的實現(xiàn)
概述
今天在做 Vue 移動端項目的時候遇到了滾動穿透問題,在網(wǎng)上查資料后,選取了我覺得最好的方法,記錄下來供以后開發(fā)時參考,相信對其他人也有用。
上層無需滾動
如果上層無需滾動的話,直接屏蔽上層的 touchmove 事件即可。示例如下:
<div @touchmove.prevent>我是里面的內(nèi)容</div>
上層需要滾動
如果上層需要滾動的話,那么固定的時候先獲取 body 的滑動距離,然后用 fixed 固定,用 top 模擬滾動距離;不固定的時候用獲取 top 的值,然后讓 body 滾動到之前的地方即可。示例如下:
<template> <div @click='handleHambergerClick'></div></template><script>export default { name: ’BaseHeaderMobile’, data() { return { isHeaderVisible: false, }; }, methods: { handleHambergerClick() { // hack: 滑動穿透問題 if (!this.isHeaderVisible) { this.lockBody(); } else { this.resetBody(); } this.isHeaderVisible = !this.isHeaderVisible; }, lockBody() { const { body } = document; const scrollTop = document.body.scrollTop || document.documentElement.scrollTop; body.style.position = ’fixed’; body.style.width = ’100%’; body.style.top = `-${scrollTop}px`; }, resetBody() { const { body } = document; const { top } = body.style; body.style.position = ’’; body.style.width = ’’; body.style.top = ’’; document.body.scrollTop = -parseInt(top, 10); document.documentElement.scrollTop = -parseInt(top, 10); }, },};</script>
到此這篇關(guān)于Vue項目移動端滾動穿透問題的實現(xiàn)的文章就介紹到這了,更多相關(guān)Vue 移動端滾動穿透內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 淺談SpringMVC jsp前臺獲取參數(shù)的方式 EL表達(dá)式2. 關(guān)于Ajax跨域問題及解決方案詳析3. jsp+servlet簡單實現(xiàn)上傳文件功能(保存目錄改進(jìn))4. 刪除docker里建立容器的操作方法5. JavaScript實現(xiàn)組件化和模塊化方法詳解6. .Net Core和RabbitMQ限制循環(huán)消費的方法7. ASP.NET MVC遍歷驗證ModelState的錯誤信息8. SpringMVC+Jquery實現(xiàn)Ajax功能9. jsp網(wǎng)頁實現(xiàn)貪吃蛇小游戲10. ASP中if語句、select 、while循環(huán)的使用方法
