最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue實(shí)現(xiàn)滑動(dòng)切換效果(僅在手機(jī)模式下可用)

 更新時(shí)間:2020年06月29日 15:02:11   作者:tjzc1352640  
這篇文章主要為大家詳細(xì)介紹了vue實(shí)現(xiàn)滑動(dòng)切換效果,僅在手機(jī)模式下可用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了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>

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue3中引入class類的寫法代碼示例

    vue3中引入class類的寫法代碼示例

    最近一直在做vue項(xiàng)目,從網(wǎng)上搜索到的資料不太多,這篇文章主要給大家介紹了關(guān)于vue3中引入class類的寫法的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-05-05
  • Vue動(dòng)態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對(duì)象)

    Vue動(dòng)態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對(duì)象)

    這篇文章主要介紹了Vue動(dòng)態(tài)擴(kuò)展表頭的表格及數(shù)據(jù)方式(數(shù)組嵌套對(duì)象),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-03-03
  • 教你利用Vue3模仿Windows窗口

    教你利用Vue3模仿Windows窗口

    最近學(xué)習(xí)了Vue3,利用vue3做了個(gè)好玩的項(xiàng)目,所以下面這篇文章主要給大家介紹了關(guān)于如何利用Vue3模仿Windows窗口的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • vue項(xiàng)目使用高德地圖的定位及關(guān)鍵字搜索功能的實(shí)例代碼(踩坑經(jīng)驗(yàn))

    vue項(xiàng)目使用高德地圖的定位及關(guān)鍵字搜索功能的實(shí)例代碼(踩坑經(jīng)驗(yàn))

    這篇文章主要介紹了vue項(xiàng)目使用高德地圖的定位及關(guān)鍵字搜索功能的實(shí)例代碼,也是小編踩了無數(shù)坑總結(jié)出來的經(jīng)驗(yàn),非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03
  • Vue通過moment插件實(shí)現(xiàn)獲取當(dāng)前月的第一天和最后一天

    Vue通過moment插件實(shí)現(xiàn)獲取當(dāng)前月的第一天和最后一天

    這篇文章主要介紹了Vue 結(jié)合插件moment 實(shí)現(xiàn)獲取當(dāng)前月的第一天和最后一天,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2023-10-10
  • Vue操作數(shù)組的幾種常用方法小結(jié)

    Vue操作數(shù)組的幾種常用方法小結(jié)

    本文主要介紹了Vue操作數(shù)組的幾種常用方法小結(jié),主要包括map、filter、forEach、find 和 findIndex 、some 和 every、includes、Array.from這幾種方法,感興趣的可以了解一下
    2023-09-09
  • Vue父子組件之間事件通信示例解析

    Vue父子組件之間事件通信示例解析

    這篇文章主要介紹了React中父子組件通信詳解,在父組件中,為子組件添加屬性數(shù)據(jù),即可實(shí)現(xiàn)父組件向子組件通信,文章通過圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2023-03-03
  • vue項(xiàng)目API接口get請(qǐng)求傳遞參數(shù)方式

    vue項(xiàng)目API接口get請(qǐng)求傳遞參數(shù)方式

    這篇文章主要介紹了vue項(xiàng)目API接口get請(qǐng)求傳遞參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Vue 計(jì)算屬性 computed

    Vue 計(jì)算屬性 computed

    這篇文章主要介紹了Vue 計(jì)算屬性 computed,一般情況下屬性都是放到data中的,但是有些屬性可能是需要經(jīng)過一些邏輯計(jì)算后才能得出來,那么我們可以把這類屬性變成計(jì)算屬性,下面我們來看看具體實(shí)例,需要的朋友可以參考一下
    2021-10-10
  • Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果

    Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果

    這篇文章主要介紹了Vue實(shí)現(xiàn)兄弟組件間的聯(lián)動(dòng)效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-01-01

最新評(píng)論

垦利县| 青河县| 老河口市| 湟中县| 黄陵县| 陆良县| 阳东县| 南丰县| 年辖:市辖区| 和林格尔县| 门头沟区| 辰溪县| 肇源县| 公安县| 维西| 梅州市| 德庆县| 五峰| 东丰县| 哈巴河县| 石首市| 天柱县| 名山县| 韶山市| 通许县| 高青县| 依安县| 尼玛县| 攀枝花市| 青铜峡市| 南川市| 新丰县| 新巴尔虎右旗| 德阳市| 乌兰浩特市| 河北省| 同心县| 教育| 东至县| 涞水县| 凌源市|