vue swiper 制作导航栏

138 篇文章 1 订阅
4 篇文章 0 订阅

先看效果

在这里插入图片描述

代码如下:

<template>
  <div class="sec-history">
    <div class="wrap-year-swiper">
      <swiper class="year-swiper" :options="swiperOption" ref="yearSwiper">
        <div class="swiper-slide yearList"
             id="yearList"
             ref="yearList"
             :key="index"
             v-for="(year, index) in yearList"
             @click="changeYear(index)"
             :class="{active: yearActiveIndex === index}">
          {{year}}
        </div>
      </swiper>
      <div class="swiper-button-prev" slot="button-prev"></div>
      <div class="swiper-button-next" slot="button-next"></div>
    </div>
    <swiper :options="courseSwiperOption" ref="courseSwiper">
      <div class="swiper-slide" :key="course" v-for="course in yearList">
        <p v-html="course"></p>
      </div>
    </swiper>
  </div>
</template>

<script>
import 'swiper/css/swiper.css'
import { Swiper, SwiperSlide } from 'vue-awesome-swiper'

export default {
  name: 'HelloWorld',
  components: { Swiper, SwiperSlide },
  data(){
    let self = this;
    this.$nextTick(()=>{
      let that = this;
    })
    return{
      yearActiveIndex: 0, // 选中的年
      yearList:[2020,2019,2018,2017,2016,2015,2014,2013,2012],
      swiperOption: {
        navigation: {
          nextEl: '.swiper-button-next',
          prevEl: '.swiper-button-prev',
        },
        slidesPerView: 6, // 左右箭头中的内容总共有几个可见
        slidesOffsetBefore: 5, // 设定slide与左边框的预设偏移量(单位px)
        slidesOffsetAfter: -8, // 设定slide与右边框的预设偏移量(单位px)
        observer: true,
      },
      courseSwiperOption: {
        autoHeight: 'auto', // 自动高度。设置为true时,wrapper和container会随着当前slide的高度而发生变化
        on: {
          slideChange: function() {
            self.yearActiveIndex = this.activeIndex;
            self.$refs.yearSwiper.$swiper.slideTo(this.activeIndex);
          },
        },
      },
    }
  },
  methods:{
    // 点击年份切换内容
    changeYear(yearActiveIndex) {
      this.yearActiveIndex = yearActiveIndex;
      this.$refs.courseSwiper.$swiper.slideTo(yearActiveIndex);
    }
  },
}
</script>

<style scoped lang="scss">
.sec-history {
  width: 100%;
  margin-bottom: 30px;
  .wrap-year-swiper {
    width: 100%;
    position: relative;
    background: #F2F2F2;
    height: 44px;
    line-height: 44px;
    margin-bottom: 15px;

    .swiper-button-prev, .swiper-button-next {
      outline: none;
    }

    .swiper-button-prev {
      left: 0;
    }

    .swiper-button-next {
      right: 0;
    }

    .swiper-button-prev:after, .swiper-button-next:after {
      font-size: 16px;
      color: #333;
    }

    .year-swiper {
      .active {
        color: #2F7DD6;
      }
    }

    .swiper-container {
      width: 86%;
    }
  }
}
</style>

对应的版本为Vue2 和 swipe5

package.json 中需要安装对应的版本

    "node-sass": "^4.14.1",
    "sass": "^1.26.5",
    "sass-loader": "^7.3.1",
    "swiper": "^5.4.5",
    "vue": "^2.6.11",
    "vue-awesome-swiper": "^4.1.1"
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值