具体代码如下所述:
<strong>组件部分</strong> <template> <div class="main"> <div class="page-tab"> <div :class="nowPath == item.path " 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>调用组件部分</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实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
更新日志
2024年11月13日
2024年11月13日
- 宝可梦大集结国服公测有多少只宝可梦 大集结国服宝可梦一览
- 国外渲染大佬《FF7RE》新图赏
- 《夺宝奇兵》Xbox长实机公布:游戏玩法全面展示
- 《剑星》X《尼尔》联动官方图赏:永远看不腻的2B
- 甲子慧.2015-最爱【乾坤唱片】【WAV+CUE】
- 潘美辰.1993-找一个雨天【蓝与白】【WAV+CUE】
- 黄舒骏.1997-非常原创精神(南方金点系列)2CD【南方】【WAV+CUE】
- Yiruma李闰珉《怀旧十周年纪念精选集》【WAV分轨】
- 晓欣《醉爱DSD》[WAV+CUE]
- 【爵士萨克斯】RichardElliot-2016-SummerMadness【24bit-FLAC】
- 谭艳《最走心好声音HQ》马兰士德国黑胶[低速原抓WAV+CUE][1G]
- 群星《第6届 2010十大发烧唱片精选》 2CD [WAV+CUE][1.5G]
- 李梦瑶&张玮伽&谭艳&孙露 -《发烧四大天后2CD》DTS丽声 2CD [WAV分轨][2G]
- 原神海浪中的莎孚旁的幻写灵解锁方法
- 【阴阳师手游】原神5.2联动介绍