移动端滑动切换组件封装 vue-swiper-router实例详解

 更新时间:2018年11月25日 11:35:23   作者:张大伟丶  
这篇文章主要介绍了移动端滑动切换组件封装 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>调用组件部分</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实例详解,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

相关文章

  • Vue3配置bem样式架构的代码详解

    Vue3配置bem样式架构的代码详解

    BEM是一种前端命名方法论,主要是针对CSS,意思是块(Block)、元素(Element)、修饰符(Modifier)的简写,这种命名方法让CSS便于统一团队开发规范和方便维护,本文给大家介绍了Vue3配置bem样式架构,需要的朋友可以参考下
    2024-10-10
  • 解决父组件将子组件作为弹窗调用只执行一次created的问题

    解决父组件将子组件作为弹窗调用只执行一次created的问题

    这篇文章主要介绍了解决父组件将子组件作为弹窗调用只执行一次created的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • Vue实现导入Excel功能步骤详解

    Vue实现导入Excel功能步骤详解

    这篇文章主要介绍了Vue实现导入Excel功能,本文分步骤给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-07-07
  • Vue.JS实现垂直方向展开、收缩不定高度模块的JS组件

    Vue.JS实现垂直方向展开、收缩不定高度模块的JS组件

    这篇文章主要介绍了Vue.JS实现垂直方向展开、收缩不定高度模块的JS组件,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2018-06-06
  • Vue 项目中如何使用fullcalendar 时间段选择插件(类似课程表格)

    Vue 项目中如何使用fullcalendar 时间段选择插件(类似课程表格)

    最近完成一个项目,需要选择一个会议室,但是最好能够通过在图上显示出该 会议室在某某时间段内已经被预定了,初看这个功能感觉很棘手,仔细分析下实现起来还是挺容易的,下面通过示例代码讲解Vue项目中使用fullcalendar时间段选择插件,感兴趣的朋友一起看看吧
    2024-07-07
  • 使用Vue3和p5.js实现交互式图像动画

    使用Vue3和p5.js实现交互式图像动画

    这篇文章主要介绍了如何用Vue3和p5.js打造一个交互式图像动画,文中给出了详细的代码示例,本代码适用于需要在网页中实现图像滑动效果的场景,例如图片浏览、相册展示等,感兴趣的小伙伴跟着小编一起来看看吧
    2024-06-06
  • vue项目启动端口更改的实现

    vue项目启动端口更改的实现

    在Vue前端项目中,可以通过修改配置文件来指定启动的端口号,本文就来介绍 一下vue项目启动端口更改的实现,感兴趣的可以了解一下
    2023-10-10
  • Vue3解决Mockjs引入后并访问404(Not Found) 的页面报错问题

    Vue3解决Mockjs引入后并访问404(Not Found) 的页面报错问题

    mock.js:是一款模拟数据生成器,可以生成随机数据,拦截 Ajax 请求,使用mockjs模拟后端接口,可随机生成所需数据,模拟对数据的增删改查,本文给大家介绍了Vue3解决Mockjs引入后并访问404(Not Found) 的页面报错问题,需要的朋友可以参考下
    2025-04-04
  • Vue实现双向数据绑定

    Vue实现双向数据绑定

    这篇文章主要为大家详细介绍了Vue实现双向数据绑定的方式,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-05-05
  • 深入浅析Vue.js计算属性和侦听器

    深入浅析Vue.js计算属性和侦听器

    这篇文章主要介绍了Vue.js计算属性和侦听器的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2018-05-05

最新评论