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

vue-router 手勢滑動觸發(fā)返回功能

 更新時間:2018年09月30日 08:45:28   作者:楊 小匠  
這篇文章主要介紹了vue-router 手勢滑動觸發(fā)返回功能,文中通過實(shí)例代碼給大家介紹了vue圖片左右滑動及手勢縮放,需要的朋友可以參考下

vue-router的路由變換只存在“變換前”和“變換后”,不存在“切換中”的狀態(tài),所以做不到大多數(shù)app(微信那樣的)在滑動過程中讓界面跟隨手指移動。但滑動事件還是可以監(jiān)聽的,我們可以在滑動之后再觸發(fā)路由回退事件。

  微博的滑動返回基本上就是這樣的原理:先滑動、再觸發(fā)返回事件,但用起來很是怪異,有嚴(yán)重的滯后感??淇藶g覽器做的就比較好:一是滑動時界面雖然不動,但是界面上有小圖標(biāo)提示,能讓用戶接受到反饋;二是返回過程很快,沒有多余的過渡動畫。

  app.vue文件如下:

<template>
 <div id="app" v-on:touchstart="bodyTouchStart" v-on:touchmove="bodyTouchMove" v-on:touchend="bodyTouchEnd">
 <transition :name="direction">
  <keep-alive include="home">
  <router-view class="appView"></router-view>
  </keep-alive>
 </transition>
 </div>
</template>

<script>
var swidth = document.documentElement.clientWidth;

export default {
 name: 'app',
 data: () => ({
 // direction 頁面切換的過渡動畫,配合transition組件使用
 direction: "slide-left",
 // touchLeft 劃動起點(diǎn)界限,起點(diǎn)在靠近屏幕左側(cè)時才有效
 touchLeft: swidth*2/5,
 // touchStartPoint 記錄起始點(diǎn)X坐標(biāo)
 touchStartPoint: 0,
 // distance 記錄劃動的距離
 distance: 0,
 // 回退按鈕的dom,根據(jù)頁面上是否存在此dom來判斷該路由是否可回退
 backBtn: null
 }),

 watch: {
 // 監(jiān)聽路有變化,決定頁面過渡動畫
 $route(to, from) {
  if (from.name == "login" || from.path.indexOf("home") > -1) {
  this.direction = "slide-left";
  } else if (to.path.indexOf("home") > -1) {
  this.direction = "slide-right";
  } else {
  const toDepth = to.path.split("/").length;
  const fromDepth = from.path.split("/").length;
  this.direction = toDepth < fromDepth ? "slide-right" : "slide-left";
  }
 }
 },

 methods: {
 bodyTouchStart: function(event) {
  this.backBtn = document.getElementById("navback");
  if (this.backBtn) {
  // 獲得起點(diǎn)X坐標(biāo),初始化distance為0
  this.touchStartPoint = event.targetTouches[0].pageX;
  this.distance = 0;
  }
 },
 bodyTouchMove: function(event) {
  if (this.backBtn && this.touchStartPoint < this.touchLeft) {
  // 只監(jiān)聽單指劃動,多指劃動不作響應(yīng)
  if (event.targetTouches.length > 1) {
   return;
  }
  // 實(shí)時計(jì)算distance
  this.distance = event.targetTouches[0].pageX - this.touchStartPoint;
  // 根據(jù)distance在頁面上做出反饋。這里演示通過返回按鈕的背景變化作出反饋
  if (this.distance > 0 && this.distance < 100) {
   this.backBtn.style.backgroundPosition = ((this.distance - 100) / 100) * 50 + "px 0";
  } else if (this.distance >= 100) {
   this.backBtn.style.backgroundPosition = "0 0";
  } else {
   this.backBtn.style.backgroundPosition = "-50px 0";
  }
  }
 },
 bodyTouchEnd: function(event) {
  if (this.backBtn && this.touchStartPoint < this.touchLeft) {
  // 劃動結(jié)束,重置數(shù)據(jù)
  this.touchStartPoint = 0;
  this.backBtn.style.backgroundPosition = "-50px 0";
  // 當(dāng)劃動距離超過100px時,觸發(fā)返回事件
  if (this.distance > 100) {
   // 返回前修改樣式,讓過渡動畫看起來更快
   document.getElementById("app").classList.add("quickback");
   this.$router.back();
   setTimeout(function(){
   document.getElementById("app").classList.remove("quickback");
   },250)
  }
  }
 }
 }
}
</script>

<style>
#app {
 -webkit-font-smoothing: antialiased;
 -moz-osx-font-smoothing: grayscale;
 width: 100%;
 overflow-x: hidden;
}
.appView {
 position: absolute;
 width: 100%;
 background: #fff;
 min-height: 100vh;
 transition: transform 0.24s ease-out;
}
#app.quickback .appView{
 transition-duration: 0.1s;
}
.slide-left-enter {
 transform: translate(100%, 0);
}
.slide-left-leave-active {
 transform: translate(-50%, 0);
}
.slide-right-enter {
 transform: translate(-50%, 0);
}
.slide-right-leave-active {
 transform: translate(100%, 0);
}
</style>

下面看下vue圖片左右滑動及手勢縮放

引入vue-awesome-swiperimport 'swiper/dist/css/swiper.css';

import { swiper, swiperSlide } from 'vue-awesome-swiper';components: {
 swiper,
 swiperSlide,
},data() {
 return {
 swiperOption: {
  width: window.innerWidth,
  zoom : true,
  initialSlide: 0,
 },
 };
},<swiper :options="swiperOption" ref="imgOverview" style="height: 100%;">
 <swiper-slide v-for="(img, index) in previewImg">
 <div class="swiper-zoom-container">
  <img :src="img" alt="">
 </div>
 </swiper-slide>
</swiper>

代碼案例見 https://github.com/yource/VueSPA

總結(jié)

以上所述是小編給大家介紹的vue-router 手勢滑動觸發(fā)返回功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • 基于Vue實(shí)現(xiàn)手勢簽名

    基于Vue實(shí)現(xiàn)手勢簽名

    這篇文章主要為大家詳細(xì)介紹了基于Vue實(shí)現(xiàn)手勢簽名,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 詳解Vue?自定義hook?函數(shù)

    詳解Vue?自定義hook?函數(shù)

    這篇文章主要介紹了詳解Vue自定義hook函數(shù),hook函數(shù)本質(zhì)是一個函數(shù),把setup函數(shù)中使用的Composition?API進(jìn)行了封裝,更多相關(guān)內(nèi)容感興趣的朋友可以參考一下
    2022-06-06
  • Vue實(shí)現(xiàn)子組件向父組件傳遞多個參數(shù)的方法

    Vue實(shí)現(xiàn)子組件向父組件傳遞多個參數(shù)的方法

    在Vue框架中,組件間的通信是一個常見的需求,特別是在子組件需要向父組件傳遞多個參數(shù)時,合理的通信方式可以顯著提升代碼的可讀性和可維護(hù)性,本文將詳細(xì)介紹如何在Vue中實(shí)現(xiàn)子組件向父組件傳遞多個參數(shù),需要的朋友可以參考下
    2024-10-10
  • 解析Vue.js中的組件

    解析Vue.js中的組件

    組件(Component)是 Vue.js 最強(qiáng)大的功能之一。組件可以擴(kuò)展 HTML 元素,封裝可重用的代碼。這篇文章主要介紹了vue.js 中的組件,需要的朋友參考下
    2018-02-02
  • Vue3之組件狀態(tài)保持KeepAlive的簡單使用

    Vue3之組件狀態(tài)保持KeepAlive的簡單使用

    這篇文章主要介紹了Vue3之組件狀態(tài)保持KeepAlive的簡單使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法

    Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法

    這篇文章主要介紹了Vue中的驗(yàn)證登錄狀態(tài)的實(shí)現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • vue單元格多列合并的實(shí)現(xiàn)

    vue單元格多列合并的實(shí)現(xiàn)

    這篇文章主要介紹了vue單元格多列合并的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • antd日期選擇器禁止選擇當(dāng)天之前的時間操作

    antd日期選擇器禁止選擇當(dāng)天之前的時間操作

    這篇文章主要介紹了antd日期選擇器禁止選擇當(dāng)天之前的時間操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue如何實(shí)現(xiàn)垂直居中

    vue如何實(shí)現(xiàn)垂直居中

    這篇文章主要介紹了vue如何實(shí)現(xiàn)垂直居中,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的示例代碼

    Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的示例代碼

    這篇文章主要介紹了Vue快速實(shí)現(xiàn)通用表單驗(yàn)證的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-01-01

最新評論