Vue解決移動(dòng)端彈窗滾動(dòng)穿透問題
參考了網(wǎng)上一大堆的解決方法,大可分為三類方法。經(jīng)過認(rèn)真的思考和分析,個(gè)人的總結(jié)如下:
使用js去控制和改變css1. 彈窗出現(xiàn)1.1. 記錄點(diǎn)擊出現(xiàn)彈窗按鈕位置的scrollTop1.2. 給body樣式{’overflow’: ’hidden’}2. 彈窗關(guān)閉2.1. 取消body樣式{’overflow’: ’hidden’}2.2. 給body樣式{’top’: scrollTop} 優(yōu)點(diǎn):實(shí)現(xiàn)簡單快捷缺點(diǎn):在彈窗一開一關(guān)的時(shí)間段,如果彈窗不是沾滿整個(gè)窗口,則會(huì)看到body閃爍 使用js去控制彈窗內(nèi)容區(qū)的默認(rèn)滾動(dòng)事件
1. 彈窗出現(xiàn) 1.1. 監(jiān)聽內(nèi)容容器layoutBox的touchstart和touchmove事件 1.2. 監(jiān)聽touchstart事件,得知手指開始滾動(dòng)內(nèi)容區(qū)的起始位置targetY 1.3. 監(jiān)聽touchmove事件,得知滾動(dòng)內(nèi)容區(qū)的過程中,變化的位置newTargetY 1.4. 拿到 內(nèi)容滾動(dòng)到容器頂部的距離 scrollTop / 內(nèi)容可滾動(dòng)的高度 scrollHeight / 當(dāng)前容器的高度 clientHeight 1.5. 在滾動(dòng)到頂部和滾動(dòng)到底部時(shí),阻止內(nèi)容容器的默認(rèn)行為。(關(guān)鍵點(diǎn))2. 彈窗正常關(guān)閉優(yōu)點(diǎn):從出現(xiàn)滾動(dòng)穿透問題的源頭出發(fā),把問題解決,js實(shí)現(xiàn)不存在ios兼容問題缺點(diǎn):實(shí)機(jī)驗(yàn)證,個(gè)別品牌的機(jī)型存在兼容性問題 彈窗內(nèi)容區(qū)禁止?jié)L動(dòng),使用js模擬滾動(dòng)條
1. 彈窗出現(xiàn) 1.1. 監(jiān)聽touchmove事件,全程阻止默認(rèn)行為 1.2. 監(jiān)聽touchstart和touchmove事件記錄手指的移動(dòng)距離,使用transform: translate3d()屬性,實(shí)現(xiàn)內(nèi)容滾動(dòng) 2. 彈窗正常關(guān)閉優(yōu)點(diǎn):js實(shí)現(xiàn)不存在ios兼容問題缺點(diǎn):ios上丟失了原生滾動(dòng)條的回彈體驗(yàn)三、初步實(shí)現(xiàn)
寫成一個(gè)mixin
/** * @author cunhang_wei * @description 解決彈窗內(nèi)容區(qū)滾動(dòng)穿透到body的問題 * @param $refs.layoutBox 需要事先指定 內(nèi)容容器 */export default { data () { return { targetY: 0 } }, mounted () { if (this.$refs.layoutBox) { this.$el.addEventListener(’touchstart’, this.handleTouchstart) this.$el.addEventListener(’touchmove’, this.handleTouchmove, false) } }, methods: { handleTouchstart (e) { this.targetY = Math.floor(e.targetTouches[0].clientY) // 手指起始觸摸位置 console.log(’handleTouchstart’, this.targetY) }, handleTouchmove (e) { let layoutBox = this.$refs.layoutBox // 內(nèi)容容器 let newTargetY = Math.floor(e.targetTouches[0].clientY) // 手指滑動(dòng)中觸摸位置 let sTop = layoutBox.scrollTop // 內(nèi)容滾動(dòng)到容器頂部的高度 let sHeight = layoutBox.scrollHeight // 內(nèi)容的可滾動(dòng)高度 let cliHeight = layoutBox.clientHeight // 當(dāng)前內(nèi)容容器的高度 if (sTop <= 0 && newTargetY - this.targetY > 0 && e.cancelable) {console.log(’下拉到頁面頂部’)e.preventDefault() } else if (sTop >= sHeight - cliHeight && newTargetY - this.targetY < 0 && e.cancelable) {console.log(’上翻到頁面底部’)e.preventDefault() } } }, beforeDestroy () { if (this.$refs.layoutBox) { this.$el.removeEventListener(’touchstart’, this.handleTouchstart) this.$el.removeEventListener(’touchmove’, this.handleTouchmove) } }}
寫完后發(fā)現(xiàn)每一次控制彈窗的滾動(dòng)穿透,都需要引入這個(gè) mixin 文件,未免有些累贅,查看了Vue的官方文檔,發(fā)現(xiàn)了一種更好的辦法,那就是 全局指令
四、寫法優(yōu)化寫成一個(gè)全局指令 no-through
/** * @author cunhang_wei * @description 解決彈窗內(nèi)容區(qū)滾動(dòng)穿透到body的問題(覆蓋率90%) **/// @description 用法// <ul v-no-through>// <li></li>// <li></li>//</ul>// 使用vuex的保存一個(gè)全局變量import state from ’src/vuex/modules/home/state’export default { name: ’no-through’, bind: function (el, binding) { const handleTouchstart = function (event) { state.targetY = Math.floor(event.targetTouches[0].clientY) // 手指起始觸摸位置 } const handleTouchmove = function (event) { let newTargetY = Math.floor(event.targetTouches[0].clientY) // 手指滑動(dòng)中觸摸位置 let sTop = el.scrollTop // 內(nèi)容滾動(dòng)到容器頂部的高度 let sHeight = el.scrollHeight // 內(nèi)容的可滾動(dòng)高度 let cliHeight = el.clientHeight // 當(dāng)前內(nèi)容容器的高度 if (sTop <= 0 && newTargetY - state.targetY > 0 && event.cancelable) {console.log(’下拉到頁面頂部’)event.preventDefault() } else if (sTop >= sHeight - cliHeight && newTargetY - state.targetY < 0 && event.cancelable) {console.log(’上翻到頁面底部’)event.preventDefault() } } el.addEventListener(’touchstart’, handleTouchstart) el.addEventListener(’touchmove’, handleTouchmove, false) }, unbind: function (el, binding) { // 重置全局變量 targetY state.targetY = 0 }}// 最后再去 main.js 注冊為全局指令,即可使用。實(shí)機(jī)測試 ios 測試通過 ios13 小米、紅米手機(jī) 測試通過 安卓10 一加手機(jī) 測試通過 安卓10 華為手機(jī)測試通過 emui11 安卓10 三星S8上存在兼容問題 (初略估計(jì)和 Samsung webView的底層實(shí)現(xiàn)有關(guān))總結(jié) 解決問題關(guān)鍵在于:要清楚的知道 什么情況下才會(huì)發(fā)生滾動(dòng)穿透 寫法的優(yōu)化,可以簡潔代碼,讓代碼更優(yōu)雅
以上就是Vue解決移動(dòng)端彈窗滾動(dòng)穿透問題的詳細(xì)內(nèi)容,更多關(guān)于vue 解決彈窗滾動(dòng)穿透的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 通過CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效2. CSS3中Transition屬性詳解以及示例分享3. 阿里前端開發(fā)中的規(guī)范要求4. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案5. 利用CSS3新特性創(chuàng)建透明邊框三角6. XML入門的常見問題(二)7. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)8. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案9. XML入門的常見問題(一)10. 解析原生JS getComputedStyle
