基于Vue實(shí)現(xiàn)頁(yè)面切換左右滑動(dòng)效果
基于Vue的頁(yè)面切換左右滑動(dòng)效果,具體內(nèi)容如下
HTML文本頁(yè)面:
<template> <div id="app> <transition :name="direction" mode="out-in"> <!--動(dòng)態(tài)獲得transition 的name值--> <router-view class="app-view" wechat-title></router-view> </transition> </div> </template>
JS定義代碼:定義在全局js文件里面
router.beforeEach((to, from, next) => { const list = ['home', 'group', 'user'] // 將需要切換效果的路由名稱組成一個(gè)數(shù)組 const toName = to.name // 即將進(jìn)入的路由名字 const fromName = from.name // 即將離開的路由名字 const toIndex = list.indexOf(toName) // 進(jìn)入下標(biāo) const fromIndex = list.indexOf(fromName) // 離開下標(biāo) let direction = '' if (toIndex > -1 && fromIndex > -1) { // 如果下標(biāo)都存在 if (toIndex < fromIndex) { // 如果進(jìn)入的下標(biāo)小于離開的下標(biāo),那么是左滑 direction = 'left' } else { direction = 'right' // 如果進(jìn)入的下標(biāo)大于離開的下標(biāo),那么是右滑 } } store.state.viewDirection = direction //這里使用vuex進(jìn)行賦值 return next() })
在App.vue文件中,進(jìn)行計(jì)算屬性:
computed: { direction () { const viewDir = this.$store.state.viewDirection let tranName = '' if (viewDir === 'left') { tranName = 'view-out' } else if (viewDir === 'right') { tranName = 'view-in' } else { tranName = 'fade' } return tranName }, },
css3進(jìn)行動(dòng)畫效果定義:使用sass
待定義引入樣式文件:
// Variables $AnimateHook: "animated"; $AnimateDuration: 0.8s; // Mixins // Base Style .#{$AnimateHook} { -webkit-animation-duration: $AnimateDuration; animation-duration: $AnimateDuration; -webkit-animation-fill-mode: both; animation-fill-mode: both; // Modifier for infinite repetition &.infinite { -webkit-animation-iteration-count: infinite; animation-iteration-count: infinite; } } // Slide @-webkit-keyframes slideInLeft { from { -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0); visibility: visible; } to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } } @keyframes slideInLeft { from { -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0); visibility: visible; } to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } } .slideInLeft { -webkit-animation-name: slideInLeft; animation-name: slideInLeft; } @-webkit-keyframes slideInRight { from { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0); visibility: visible; } to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } } @keyframes slideInRight { from { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0); visibility: visible; } to { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } } .slideInRight { -webkit-animation-name: slideInRight; animation-name: slideInRight; } @-webkit-keyframes slideOutLeft { from { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } to { visibility: hidden; -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0); } } @keyframes slideOutLeft { from { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } to { visibility: hidden; -webkit-transform: translate3d(-100%, 0, 0); transform: translate3d(-100%, 0, 0); } } .slideOutLeft { -webkit-animation-name: slideOutLeft; animation-name: slideOutLeft; } @-webkit-keyframes slideOutRight { from { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } to { visibility: hidden; -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0); } } @keyframes slideOutRight { from { -webkit-transform: translate3d(0, 0, 0); transform: translate3d(0, 0, 0); } to { visibility: hidden; -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0); } } .slideOutRight { -webkit-animation-name: slideOutRight; animation-name: slideOutRight; } @-webkit-keyframes inRight { 0% { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0) } to { -webkit-transform: translateZ(0); transform: translateZ(0) } } @keyframes inRight { 0% { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0) } to { -webkit-transform: translateZ(0); transform: translateZ(0) } } @-webkit-keyframes outLeft { 0% { -webkit-transform: translateZ(0); transform: translateZ(0) } to { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0) } } @keyframes outLeft { 0% { -webkit-transform: translateZ(0); transform: translateZ(0) } to { -webkit-transform: translate3d(100%, 0, 0); transform: translate3d(100%, 0, 0) } }
定義進(jìn)入與離開的動(dòng)畫過渡:
.fade-enter-active, .fade-leave-active { transition: all .2s ease; } .fade-enter, .fade-leave-active { opacity: 0; } .view-in-enter-active, .view-out-leave-active { position: absolute; top: 0; width: 100%; padding-top: px2rem($titbarHeight); -webkit-animation-duration: .3s; animation-duration: .3s; -webkit-animation-fill-mode: both; animation-fill-mode: both; -webkit-backface-visibility: hidden; backface-visibility: hidden; } .view-in-enter-active { -webkit-animation-name: inRight; animation-name: inRight; } .view-out-leave-active { -webkit-animation-name: outLeft; animation-name: outLeft; }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- vue使用swiper實(shí)現(xiàn)左右滑動(dòng)切換圖片
- vue實(shí)現(xiàn)頁(yè)面切換滑動(dòng)效果
- vue實(shí)現(xiàn)滑動(dòng)切換效果(僅在手機(jī)模式下可用)
- 移動(dòng)端滑動(dòng)切換組件封裝 vue-swiper-router實(shí)例詳解
- vue中選項(xiàng)卡點(diǎn)擊切換且能滑動(dòng)切換功能的實(shí)現(xiàn)代碼
- 詳解vue2.0 使用動(dòng)態(tài)組件實(shí)現(xiàn) Tab 標(biāo)簽頁(yè)切換效果(vue-cli)
- 詳解使用vue實(shí)現(xiàn)tab 切換操作
- Vue.js組件tabs實(shí)現(xiàn)選項(xiàng)卡切換效果
- 基于Vue實(shí)現(xiàn)tab欄切換內(nèi)容不斷實(shí)時(shí)刷新數(shù)據(jù)功能
- vue實(shí)現(xiàn)鼠標(biāo)滑動(dòng)展示tab欄切換
相關(guān)文章
詳解Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on
Vue.js的指令是以v-開頭的,它們作用于HTML元素,指令提供了一些特殊的特性。這篇文章主要介紹了Vue的常用指令v-if, v-for, v-show,v-else, v-bind, v-on 的相關(guān)知識(shí),需要的朋友可以參考下2018-10-10一文詳解Vue3中簡(jiǎn)單diff算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹Vue3中簡(jiǎn)單diff算法的實(shí)現(xiàn)與使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-09-09vue實(shí)現(xiàn)登錄后頁(yè)面跳轉(zhuǎn)到之前頁(yè)面
本文給大家分享了vue實(shí)現(xiàn)登錄后頁(yè)面跳轉(zhuǎn)到之前頁(yè)面的一個(gè)功能,有這方便需要的朋友學(xué)習(xí)參考下吧。2018-01-01Vue登錄功能實(shí)現(xiàn)全套詳解(含封裝axios)
登錄功能對(duì)于前端剛?cè)腴T不久的同學(xué)來(lái)說(shuō)較為困難,下面這篇文章主要給大家介紹了關(guān)于Vue登錄功能實(shí)現(xiàn)(含封裝axios)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中
這篇文章主要介紹了element-ui中實(shí)現(xiàn)tree子節(jié)點(diǎn)部分選中時(shí)父節(jié)點(diǎn)也選中的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/buil
最近在npm install 的時(shí)候遇到了個(gè)問題,所以給大家總結(jié)下,下面這篇文章主要給大家介紹了關(guān)于vue在install時(shí)node-sass@4.14.1?postinstall:node?scripts/build.js錯(cuò)誤的解決方法,需要的朋友可以參考下2023-05-05vue3中?provide?和?inject?用法小結(jié)
父子組件傳遞數(shù)據(jù)時(shí),使用的是props和emit,父?jìng)髯訒r(shí),使用的是?props,如果是父組件傳孫組件時(shí),就需要先傳給子組件,子組件再傳給孫組件,如果多個(gè)子組件或多個(gè)孫組件使用時(shí),就需要傳很多次,會(huì)很麻煩,這篇文章主要介紹了vue3中?provide?和?inject?用法,需要的朋友可以參考下2023-11-11