国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

Vue項(xiàng)目移動端滾動穿透問題的實(shí)現(xiàn)

瀏覽:6日期:2023-01-20 09:39:27

概述

今天在做 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)!

標(biāo)簽: Vue
相關(guān)文章:
主站蜘蛛池模板: 安泽县| 修文县| 内江市| 云安县| 泾川县| 南乐县| 饶河县| 新蔡县| 垫江县| 禄丰县| 连州市| 法库县| 华容县| 江达县| 台南市| 会泽县| 灵石县| 白河县| 漳平市| 邵阳市| 丰县| 义乌市| 翁牛特旗| 海原县| 象州县| 南和县| 刚察县| 克什克腾旗| 拉孜县| 玛沁县| 平南县| 巨野县| 金阳县| 西和县| 黄冈市| 榆树市| 岱山县| 博湖县| 福鼎市| 克东县| 湛江市|