Vue項(xiàng)目移動(dòng)端滾動(dòng)穿透問題的實(shí)現(xiàn)
概述
今天在做 Vue 移動(dòng)端項(xiàng)目的時(shí)候遇到了滾動(dòng)穿透問題,在網(wǎng)上查資料后,選取了我覺得最好的方法,記錄下來供以后開發(fā)時(shí)參考,相信對(duì)其他人也有用。
上層無需滾動(dòng)
如果上層無需滾動(dòng)的話,直接屏蔽上層的 touchmove 事件即可。示例如下:
<div @touchmove.prevent>我是里面的內(nèi)容</div>
上層需要滾動(dòng)
如果上層需要滾動(dòng)的話,那么固定的時(shí)候先獲取 body 的滑動(dòng)距離,然后用 fixed 固定,用 top 模擬滾動(dòng)距離;不固定的時(shí)候用獲取 top 的值,然后讓 body 滾動(dòng)到之前的地方即可。示例如下:
<template> <div @click='handleHambergerClick'></div></template><script>export default { name: ’BaseHeaderMobile’, data() { return { isHeaderVisible: false, }; }, methods: { handleHambergerClick() { // hack: 滑動(dòng)穿透問題 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)目移動(dòng)端滾動(dòng)穿透問題的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)Vue 移動(dòng)端滾動(dòng)穿透內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP常用日期格式化函數(shù) FormatDate()2. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式3. PHP設(shè)計(jì)模式中工廠模式深入詳解4. ASP中實(shí)現(xiàn)字符部位類似.NET里String對(duì)象的PadLeft和PadRight函數(shù)5. XML入門的常見問題(二)6. 如何在jsp界面中插入圖片7. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法8. 利用CSS3新特性創(chuàng)建透明邊框三角9. 將properties文件的配置設(shè)置為整個(gè)Web應(yīng)用的全局變量實(shí)現(xiàn)方法10. jsp實(shí)現(xiàn)textarea中的文字保存換行空格存到數(shù)據(jù)庫(kù)的方法
