這篇文章將為大家詳細(xì)講解有關(guān)vue如何實(shí)現(xiàn)滑動(dòng)切換效果,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。
創(chuàng)新互聯(lián)公司專注于蒼南企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站建設(shè)。蒼南網(wǎng)站建設(shè)公司,為蒼南等地區(qū)提供建站服務(wù)。全流程按需規(guī)劃網(wǎng)站,專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)vue實(shí)現(xiàn)滑動(dòng)時(shí)紅黃色塊左右滑動(dòng)相應(yīng)距離,效果如下圖
實(shí)現(xiàn)過程主要在于實(shí)時(shí)跟蹤手指滑動(dòng)位置與原位置之間的偏移量,再相應(yīng)移動(dòng)紅黃塊。
紅黃塊布局如下
back中包含back-l,back-r左右兩塊,正常情況下為了隱藏其中一塊,子模塊需要設(shè)置display: inline-block,并且寬度都需要設(shè)置width: 100%。父模塊中設(shè)置white-space: nowrap用于處理兩個(gè)子模塊之間的空白。
<template lang="html"> <div class="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove" @touchend="touchEnd" ref="back"> <div class="back-l" ref="left"></div> <div class="back-r" ref="right"></div> </div> </template> <style scoped lang="stylus" rel="stylesheet/stylus"> .back position: fixed width: 100% height: 100px white-space: nowrap .back-l position: relative vertical-align: top display: inline-block width: 100% height: 100% background-color: red .back-r display: inline-block vertical-align: top position: relative width: 100% height: 100% background-color: yellow </style>
父模塊監(jiān)聽滑動(dòng)事件
滑動(dòng)事件分為三種:touchstart,touchmove,touchEnd,加上prevent避免頁(yè)面相應(yīng)滑動(dòng)。
在touchstart中記錄滑動(dòng)開始點(diǎn):
touchStart(e) { const touch = e.touches[0] this.touch.startX = touch.pageX this.touch.startY = touch.pageY }
touchmove中為滑動(dòng)過程,手指未離開頁(yè)面,離開頁(yè)面時(shí)觸發(fā)touchend。滑動(dòng)過程中,當(dāng)橫向偏離位置大于縱向偏離位置時(shí)認(rèn)為滑動(dòng)有效,記錄手指偏離位置,相應(yīng)移動(dòng)紅黃塊。
touchMove(e) { console.log("move"); const touch = e.touches[0] //橫向和縱向偏離位置 const deltaX = touch.pageX - this.touch.startX const deltaY = touch.pageY - this.touch.startY if (Math.abs(deltaY) > Math.abs(deltaX)) { return } const left = this.currentPlay == 'red' ? 0 : -window.innerWidth var offsetWidth = Math.min(0, Math.max(-window.innerWidth,left+deltaX)) //記錄滑動(dòng)的距離占屏幕寬度的百分比,如果滑動(dòng)太少則不切換 this.percent = Math.abs(offsetWidth/window.innerWidth) //移動(dòng)紅黃塊 this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` //設(shè)置動(dòng)畫時(shí)間 this.$refs.back.style["transitionDuration"] = 10 }
計(jì)算偏移量時(shí)首先需要知道當(dāng)前偏移位置,如果當(dāng)前在紅塊,初始偏移量為0,否則初始偏移量為負(fù)的屏幕寬度。初始偏移量加上橫向偏移量首先和-window.innerWidth取大值,-window.innerWidth為最左偏移量。再和0相比較取最小值,偏移量為0或者大于零則不再(向右移動(dòng))移動(dòng),小于零則可以向左移動(dòng)。
touchend中處理最終效果,如果滑動(dòng)距離不大于某一值則恢復(fù)原位,否則切換。
touchEnd() { console.log("end"); console.log(this.percent); let offsetWidth let percent //當(dāng)前為紅色,滑動(dòng)占比大于0.1則切換,否則回到原位置 if(this.currentPlay === 'red'){ if(this.percent > 0.1) { this.currentPlay = 'yellow' offsetWidth = -window.innerWidth } else { offsetWidth = 0 } } else { //當(dāng)前為黃色,滑動(dòng)占比大于0.9則切換,否則回到原位置 if(this.percent < 0.9) { this.currentPlay = 'red' offsetWidth = 0 } else { offsetWidth = -window.innerWidth } } //這里的transform是針對(duì)最開始的位置而言,而不是移動(dòng)過程中的位置 this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` this.$refs.back.style["transitionDuration"] = 10 }
完整代碼
<template lang="html"> <div class="back" @touchstart.prevent="touchStart" @touchmove.prevent="touchMove" @touchend="touchEnd" ref="back"> <div class="back-l" ref="left"></div> <div class="back-r" ref="right"></div> </div> </template> <script> export default { data() { return { currentPlay: 'red', percent: 0 } }, created() { this.touch = {} }, methods: { touchStart(e) { const touch = e.touches[0] this.touch.startX = touch.pageX this.touch.startY = touch.pageY }, touchMove(e) { console.log("move"); const touch = e.touches[0] const deltaX = touch.pageX - this.touch.startX const deltaY = touch.pageY - this.touch.startY if (Math.abs(deltaY) > Math.abs(deltaX)) { return } const left = this.currentPlay == 'red' ? 0 : -window.innerWidth var offsetWidth = Math.min(0, Math.max(-window.innerWidth,left+deltaX)) this.percent = Math.abs(offsetWidth/window.innerWidth) this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` this.$refs.back.style["transitionDuration"] = 10 }, touchEnd() { console.log("end"); console.log(this.percent); let offsetWidth let percent if(this.currentPlay === 'red'){ if(this.percent > 0.1) { this.currentPlay = 'yellow' offsetWidth = -window.innerWidth } else { offsetWidth = 0 } } else { if(this.percent < 0.9) { this.currentPlay = 'red' offsetWidth = 0 } else { offsetWidth = -window.innerWidth } } this.$refs.back.style["transform"] = `translate3d(${offsetWidth}px,0,0)` this.$refs.back.style["transitionDuration"] = 10 } } } </script> <style scoped lang="stylus" rel="stylesheet/stylus"> .back position: fixed width: 100% height: 100px white-space: nowrap .back-l position: relative vertical-align: top display: inline-block width: 100% height: 100% background-color: red .back-r display: inline-block vertical-align: top position: relative width: 100% height: 100% background-color: yellow </style>
關(guān)于“vue如何實(shí)現(xiàn)滑動(dòng)切換效果”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。
網(wǎng)頁(yè)題目:vue如何實(shí)現(xiàn)滑動(dòng)切換效果-創(chuàng)新互聯(lián)
URL分享:http://www.chinadenli.net/article0/dheioo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、網(wǎng)站導(dǎo)航、App設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、外貿(mào)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容