您好,登錄后才能下訂單哦!
這篇文章主要介紹移動端滑動切換組件封裝 vue-swiper-router的示例分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
具體代碼如下所述:
<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) { //設置向前動畫 this.slideDirection = 'slideforward'; this.$router.push({'path': this.tabList[nowRouteIndex + 1].path}); } if (disXY&&direction < 0 && nowRouteIndex > 0) { //設置向后動畫 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>
完整代碼 --> 代碼地址 移動端滑動切換
以上是“移動端滑動切換組件封裝 vue-swiper-router的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關知識,歡迎關注億速云行業(yè)資訊頻道!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。