vue-router 手勢(shì)滑動(dòng)觸發(fā)返回功能
vue-router的路由變換只存在“變換前”和“變換后”,不存在“切換中”的狀態(tài),所以做不到大多數(shù)app(微信那樣的)在滑動(dòng)過(guò)程中讓界面跟隨手指移動(dòng)。但滑動(dòng)事件還是可以監(jiān)聽(tīng)的,我們可以在滑動(dòng)之后再觸發(fā)路由回退事件。
微博的滑動(dòng)返回基本上就是這樣的原理:先滑動(dòng)、再觸發(fā)返回事件,但用起來(lái)很是怪異,有嚴(yán)重的滯后感??淇藶g覽器做的就比較好:一是滑動(dòng)時(shí)界面雖然不動(dòng),但是界面上有小圖標(biāo)提示,能讓用戶(hù)接受到反饋;二是返回過(guò)程很快,沒(méi)有多余的過(guò)渡動(dòng)畫(huà)。
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 頁(yè)面切換的過(guò)渡動(dòng)畫(huà),配合transition組件使用
direction: "slide-left",
// touchLeft 劃動(dòng)起點(diǎn)界限,起點(diǎn)在靠近屏幕左側(cè)時(shí)才有效
touchLeft: swidth*2/5,
// touchStartPoint 記錄起始點(diǎn)X坐標(biāo)
touchStartPoint: 0,
// distance 記錄劃動(dòng)的距離
distance: 0,
// 回退按鈕的dom,根據(jù)頁(yè)面上是否存在此dom來(lái)判斷該路由是否可回退
backBtn: null
}),
watch: {
// 監(jiān)聽(tīng)路有變化,決定頁(yè)面過(guò)渡動(dòng)畫(huà)
$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)聽(tīng)單指劃動(dòng),多指劃動(dòng)不作響應(yīng)
if (event.targetTouches.length > 1) {
return;
}
// 實(shí)時(shí)計(jì)算distance
this.distance = event.targetTouches[0].pageX - this.touchStartPoint;
// 根據(jù)distance在頁(yè)面上做出反饋。這里演示通過(guò)返回按鈕的背景變化作出反饋
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) {
// 劃動(dòng)結(jié)束,重置數(shù)據(jù)
this.touchStartPoint = 0;
this.backBtn.style.backgroundPosition = "-50px 0";
// 當(dāng)劃動(dòng)距離超過(guò)100px時(shí),觸發(fā)返回事件
if (this.distance > 100) {
// 返回前修改樣式,讓過(guò)渡動(dòng)畫(huà)看起來(lái)更快
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圖片左右滑動(dòng)及手勢(shì)縮放
引入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>
代碼案例見(jiàn) https://github.com/yource/VueSPA
總結(jié)
以上所述是小編給大家介紹的vue-router 手勢(shì)滑動(dòng)觸發(fā)返回功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
VUE axios發(fā)送跨域請(qǐng)求需要注意的問(wèn)題
本篇文章主要介紹了VUE axios發(fā)送跨域請(qǐng)求需要注意的問(wèn)題,在實(shí)際項(xiàng)目中前端使用到vue,后端使用php進(jìn)行開(kāi)發(fā)。前端使用axios請(qǐng)求請(qǐng)求遇到的問(wèn)題,有興趣的可以了解一下2017-07-07
vue2中的keep-alive使用總結(jié)及注意事項(xiàng)
vue2.0提供了一個(gè)keep-alive組件用來(lái)緩存組件,避免多次加載相應(yīng)的組件,減少性能消耗。本文給大家介紹vue2中的keep-alive使用總結(jié)及注意事項(xiàng),需要的朋友參考下吧2017-12-12
vue關(guān)閉瀏覽器退出登錄的實(shí)現(xiàn)示例
本文主要介紹了vue關(guān)閉瀏覽器退出登錄,一般都是根據(jù)根據(jù)beforeunload和unload這兩個(gè)事件執(zhí)行的。本文就詳細(xì)的介紹一下如何實(shí)現(xiàn),感興趣的可以了解一下2021-12-12
使用vue.js實(shí)現(xiàn)聯(lián)動(dòng)效果的示例代碼
本篇文章主要介紹了使用vue.js實(shí)現(xiàn)聯(lián)動(dòng)效果的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
vue腳手架搭建/idea執(zhí)行vue項(xiàng)目全過(guò)程
新建目錄并運(yùn)行命令提示符,通過(guò)npm安裝Vue腳手架并查看版本號(hào),接著,使用vue create命令創(chuàng)建Vue項(xiàng)目,選擇所需配置后完成項(xiàng)目創(chuàng)建,創(chuàng)建成功可見(jiàn)Vue文件夾,使用IDEA打開(kāi)項(xiàng)目,并啟動(dòng)項(xiàng)目,根據(jù)需求修改初始化界面2024-10-10
Vue?使用postMessage?實(shí)現(xiàn)父子跨域通信
這篇文章主要介紹了Vue應(yīng)用?postMessage?實(shí)現(xiàn)父子跨域通信,通過(guò)示例介紹了postMessage的使用,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vue3修改link標(biāo)簽?zāi)J(rèn)icon無(wú)效問(wèn)題詳解
這篇文章主要介紹了vue3修改link標(biāo)簽?zāi)J(rèn)icon無(wú)效問(wèn)題詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

