Vue項(xiàng)目移動端滾動穿透問題的實(shí)現(xiàn)
概述
今天在做 Vue 移動端項(xiàng)目的時(shí)候遇到了滾動穿透問題,在網(wǎng)上查資料后,選取了我覺得最好的方法,記錄下來供以后開發(fā)時(shí)參考,相信對其他人也有用。
上層無需滾動
如果上層無需滾動的話,直接屏蔽上層的 touchmove 事件即可。示例如下:
<div @touchmove.prevent>我是里面的內(nèi)容</div>
上層需要滾動
如果上層需要滾動的話,那么固定的時(shí)候先獲取 body 的滑動距離,然后用 fixed 固定,用 top 模擬滾動距離;不固定的時(shí)候用獲取 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àng)目移動端滾動穿透問題的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue 移動端滾動穿透內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP.NET MVC使用正則表達(dá)式驗(yàn)證手機(jī)號碼2. ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字3. SpringMVC+Jquery實(shí)現(xiàn)Ajax功能4. FastDFS安裝擴(kuò)展篇——安裝PHP、Apache及Nginx的FastDFS擴(kuò)展5. PHP安全-遠(yuǎn)程文件風(fēng)險(xiǎn)6. Python數(shù)據(jù)庫格式化輸出文檔的思路與方法7. php 備份數(shù)據(jù)庫類8. Vue基于iview實(shí)現(xiàn)登錄密碼的顯示與隱藏功能9. 基于SpringBoot核心原理(自動配置、事件驅(qū)動、Condition)10. 如何在springBoot下搭建日志框架
