移動端滑動切換組件封裝 vue-swiper-router實(shí)例詳解
具體代碼如下所述:
<strong>組件部分</strong>
<template>
<div class="main">
<div class="page-tab">
<div
:class="nowPath == item.path ? 'tab-item tab-item_active' : 'tab-item'"
v-for='(item, index) in tabList'
:key="index">
<router-link
mode="out-in"
:to="item.path">{{item.name}}
</router-link>
</div>
</div>
<transition :name="slideDirection">
<slot>
</slot>
</transition>
</div>
</template>
<script>
export default {
props: {
tabList: Array
},
mounted() {
this.nowPath = this.$route.path;
this.initTouchedEvent();
},
data() {
return {
tabSwiper: {},
slideDirection: 'slideforward',
nowPath: '',
startX: '',
startY:''
};
},
methods: {
touchedstartHandler(e) {
this.startX = e.changedTouches[0].pageX;
this.startY = e.changedTouches[0].pageY;
},
touchendHandler(e) {
let direction = this.startX - e.changedTouches[0].pageX;
let directionY = this.startY - e.changedTouches[0].pageY;
let nowRouteIndex = 0;
this.tabList.forEach((v, index) => {
if (v.path == this.nowPath) {
nowRouteIndex = index;
}
});
var disXY = Math.abs(direction)>Math.abs(directionY);
if (disXY&&direction >= 0 && nowRouteIndex < this.tabList.length - 1) {
//設(shè)置向前動畫
this.slideDirection = 'slideforward';
this.$router.push({'path': this.tabList[nowRouteIndex + 1].path});
}
if (disXY&&direction < 0 && nowRouteIndex > 0) {
//設(shè)置向后動畫
this.slideDirection = 'slideback';
this.$router.push({'path': this.tabList[nowRouteIndex - 1].path});
}
},
initTouchedEvent() {
this.$el.addEventListener('touchstart', this.touchedstartHandler.bind(this));
this.$el.addEventListener('touchend', this.touchendHandler.bind(this));
},
},
watch: {
'$route' (to, from) {
this.nowPath = to.path;
}
}
};
</script>
<style>
* {
margin: 0;
padding: 0;
}
body {
height: 100%;
width: 100%;
background-color: #fbf9fe;
}
a {
color: #333;
text-decoration: none;
}
.page {
display: flex;
justify-content: center;
align-items: center;
}
.page-tab {
display: flex;
justify-content: center;
}
.tab-item {
text-align: center;
align-items: center;
height: 44px;
line-height: 44px;
flex: 1;
height: 100%;
background-color: #fff;
}
.tab-item_active {
border-bottom: 3px solid #f90;
}
.tab-item_active a {
color: #f90;
}
.slideforward-enter-active, .slideforward-leave-active {
position: absolute;
transition: all .5s;
transform: translate3d(0px, 0px, 0px);
}
.slideforward-enter, .slideforward-leave-to {
position: absolute;
transform: translate3d(200px, 0px, 0px);
}
.slideback-enter-active, .slideback-leave-active {
position: absolute;
transition: all .5s;
transform: translate3d(0px, 0px, 0px);
}
.slideback-enter, .slideback-leave-to {
position: absolute;
transform: translate3d(-200px, 0px, 0px);
}
</style>
<strong>router部分</strong>
import Vue from 'vue';
import Router from 'vue-router';
import Page1 from '@/pages/page1/index';
import Page2 from '@/pages/page2/index';
import Page3 from '@/pages/page3/index';
import Page4 from '@/pages/page4/index';
Vue.use(Router)
export default new Router({
routes: [
{
path: '/',
name: 'index',
component: Page1
},
{
path: '/page2',
name: 'Page2',
component: Page2
},
{
path: '/page3',
name: 'Page3',
component: Page3
},
{
path: '/page4',
name: 'Page4',
component: Page4
}
]
});
<strong>調(diào)用組件部分</strong>
<template>
<div id="app">
<TabPages
:tab-list='tabList'>
<router-view/>
</TabPages>
</div>
</template>
<script>
import TabPages from './components/index';
export default {
name: 'app',
data() {
return {
tabList: [{
name: 'tab1',
path: '/'
}, {
name: 'tab2',
path: '/page2'
}, {
name: 'tab3',
path: '/page3'
}, {
name: 'tab4',
path: '/page4'
}]
}
},
components: {
TabPages
}
}
</script>
<style>
</style>

總結(jié)
以上所述是小編給大家介紹的移動端滑動切換組件封裝 vue-swiper-router實(shí)例詳解,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時(shí)回復(fù)大家的!
相關(guān)文章
解決父組件將子組件作為彈窗調(diào)用只執(zhí)行一次created的問題
這篇文章主要介紹了解決父組件將子組件作為彈窗調(diào)用只執(zhí)行一次created的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Vue實(shí)現(xiàn)導(dǎo)入Excel功能步驟詳解
這篇文章主要介紹了Vue實(shí)現(xiàn)導(dǎo)入Excel功能,本文分步驟給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
Vue.JS實(shí)現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件
這篇文章主要介紹了Vue.JS實(shí)現(xiàn)垂直方向展開、收縮不定高度模塊的JS組件,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-06-06
Vue 項(xiàng)目中如何使用fullcalendar 時(shí)間段選擇插件(類似課程表格)
最近完成一個項(xiàng)目,需要選擇一個會議室,但是最好能夠通過在圖上顯示出該 會議室在某某時(shí)間段內(nèi)已經(jīng)被預(yù)定了,初看這個功能感覺很棘手,仔細(xì)分析下實(shí)現(xiàn)起來還是挺容易的,下面通過示例代碼講解Vue項(xiàng)目中使用fullcalendar時(shí)間段選擇插件,感興趣的朋友一起看看吧2024-07-07
使用Vue3和p5.js實(shí)現(xiàn)交互式圖像動畫
這篇文章主要介紹了如何用Vue3和p5.js打造一個交互式圖像動畫,文中給出了詳細(xì)的代碼示例,本代碼適用于需要在網(wǎng)頁中實(shí)現(xiàn)圖像滑動效果的場景,例如圖片瀏覽、相冊展示等,感興趣的小伙伴跟著小編一起來看看吧2024-06-06
vue項(xiàng)目啟動端口更改的實(shí)現(xiàn)
在Vue前端項(xiàng)目中,可以通過修改配置文件來指定啟動的端口號,本文就來介紹 一下vue項(xiàng)目啟動端口更改的實(shí)現(xiàn),感興趣的可以了解一下2023-10-10
Vue3解決Mockjs引入后并訪問404(Not Found) 的頁面報(bào)錯問題
mock.js:是一款模擬數(shù)據(jù)生成器,可以生成隨機(jī)數(shù)據(jù),攔截 Ajax 請求,使用mockjs模擬后端接口,可隨機(jī)生成所需數(shù)據(jù),模擬對數(shù)據(jù)的增刪改查,本文給大家介紹了Vue3解決Mockjs引入后并訪問404(Not Found) 的頁面報(bào)錯問題,需要的朋友可以參考下2025-04-04
Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)雙向數(shù)據(jù)綁定的方式,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

