欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)過(guò)程主要在于實(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)聽(tīng)滑動(dòng)事件

滑動(dòng)事件分為三種:touchstart,touchmove,touchEnd,加上prevent避免頁(yè)面相應(yīng)滑動(dòng)。

在touchstart中記錄滑動(dòng)開(kāi)始點(diǎn):

touchStart(e) {
  const touch = e.touches[0]
  this.touch.startX = touch.pageX
  this.touch.startY = touch.pageY
 }

touchmove中為滑動(dòng)過(guò)程,手指未離開(kāi)頁(yè)面,離開(kāi)頁(yè)面時(shí)觸發(fā)touchend。滑動(dòng)過(guò)程中,當(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ì)最開(kāi)始的位置而言,而不是移動(dòng)過(guò)程中的位置
 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)文章

  • Vue組件的使用教程詳解

    Vue組件的使用教程詳解

    組件 (Component) 是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素,封裝可重用的代碼。這篇文章給大家介紹了vue組件的使用,需要的朋友參考下吧
    2018-01-01
  • Vue中對(duì)watch的理解(關(guān)鍵是immediate和deep屬性)

    Vue中對(duì)watch的理解(關(guān)鍵是immediate和deep屬性)

    watch偵聽(tīng)器,是Vue實(shí)例的一個(gè)屬性,是用來(lái)響應(yīng)數(shù)據(jù)的變化,需要在數(shù)據(jù)變化時(shí)執(zhí)行異步或開(kāi)銷較大的操作時(shí),這個(gè)方式是最有用的,這篇文章主要介紹了Vue中對(duì)watch的理解,需要的朋友可以參考下
    2022-11-11
  • vite+ts vite.config.ts使用path報(bào)錯(cuò)問(wèn)題及解決

    vite+ts vite.config.ts使用path報(bào)錯(cuò)問(wèn)題及解決

    這篇文章主要介紹了vite+ts vite.config.ts使用path報(bào)錯(cuò)問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • 基于Vue el-autocomplete 實(shí)現(xiàn)類似百度搜索框功能

    基于Vue el-autocomplete 實(shí)現(xiàn)類似百度搜索框功能

    本文通過(guò)代碼給大家介紹了Vue el-autocomplete 實(shí)現(xiàn)類似百度搜索框功能,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-10-10
  • vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題

    vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題

    這篇文章主要介紹了vue數(shù)組中不滿足條件跳出循環(huán)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • 詳解vue表單——小白速看

    詳解vue表單——小白速看

    大家可以用 v-model 指令在表單控件元素上創(chuàng)建雙向數(shù)據(jù)綁定。這篇文章主要介紹了vue表單詳解,需要的朋友可以參考下
    2018-04-04
  • vue prop屬性傳值與傳引用示例

    vue prop屬性傳值與傳引用示例

    今天小編就為大家分享一篇vue prop屬性傳值與傳引用示例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-11-11
  • Vue 使用超圖SuperMap的實(shí)踐

    Vue 使用超圖SuperMap的實(shí)踐

    作為一名剛?cè)腴T計(jì)算機(jī)語(yǔ)言的人來(lái)說(shuō),要想快速完成測(cè)試開(kāi)發(fā)地圖項(xiàng)目,肯定要接用到SuperMap,本文主要介紹了Vue 使用超圖SuperMap的實(shí)踐,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-12-12
  • Vue+OpenLayer實(shí)現(xiàn)測(cè)距功能

    Vue+OpenLayer實(shí)現(xiàn)測(cè)距功能

    OpenLayers?是一個(gè)專為Web?GIS?客戶端開(kāi)發(fā)提供的JavaScript?類庫(kù)包,用于實(shí)現(xiàn)標(biāo)準(zhǔn)格式發(fā)布的地圖數(shù)據(jù)訪問(wèn)。本文將通過(guò)Vue和OpenLayer實(shí)現(xiàn)測(cè)距功能?,需要的可以參考一下
    2022-04-04
  • Vue封裝一個(gè)簡(jiǎn)單輕量的上傳文件組件的示例

    Vue封裝一個(gè)簡(jiǎn)單輕量的上傳文件組件的示例

    這篇文章主要介紹了Vue封裝一個(gè)簡(jiǎn)單輕量的上傳文件組件的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論