vue swiper 自定义分页器及动效

<template>
  <div class="components-container">
    <swiper :options="mySwiperOption"
            ref="mySwiper"
            @slideChangeEnd='slideChange'>
      <swiper-slide v-for=" i in 3"
                    :key="i">
        <img :src="httpImageUrl[i-1]"
             width="100%"
             @click="onClick(i)" />
      </swiper-slide>
      <div class="swiper-pagination"
           slot="pagination"></div>
    </swiper>
    <div class="ditBox">
      <div class="dit"
           :class="{'actived':id===i}"
           v-for="i in 3"
           :key="i"
           @click="setIndex(i)"></div>
    </div>
  </div>
</template>

<script>
//页面引入swiper
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'

export default {
  name: 'ContentAudit',
  components: {
    swiper,
    swiperSlide
  },
  data () {
    let vm = this
    return {
      id: null,
      mySwiperOption: {
        pagination: {
          el: '.swiper-pagination', //与slot="pagination"处 class 一致
          clickable: true, //轮播按钮支持点击
          // paginationCustomRender: function (swiper, current, total) {
          //   var customPaginationHtml = "";
          //   for (var i = 0; i < total; i++) {
          //     //判断哪个分页器此刻应该被激活
          //     if (i == current - 1) {
          //       customPaginationHtml +=
          //         '<span class="swiper-pagination-customs swiper-pagination-customs-active"></span>';
          //     } else {
          //       customPaginationHtml +=
          //         '<span class="swiper-pagination-customs"></span>';
          //     }
          //   }
          //   return '<span class="swiperPag">' + customPaginationHtml + '</span>';
          // }
        },
        //自动播放
        autoplay: {
          delay: 3000,
          disableOnInteraction: false
        },
        //循环
        loop: true,
        on: {
          init: function (swiper) {
            //Swiper初始化了
            alert('当前的slide序号是' + this.activeIndex + '当前的realIndex序号是' + this.realIndex);//或者swiper.activeIndex,swiper与this都可指代当前swiper实例

            vm.id = this.realIndex + 1
            this.emit('transitionEnd');//在初始化时触发一次transitionEnd事件,需要先设置transitionEnd

          },
        },
        transitionEnd: function (swiper, realIndex) {
          //Swiper初始化了
          console.log('9999', this.realIndex)
          // alert('当前的slide序号是' + this.activeIndex);//或者swiper.activeIndex,swiper与this都可指代当前swiper实例
          // this.emit('transitionEnd');//在初始化时触发一次transitionEnd事件,需要先设置transitionEnd

        },
      },
      httpImageUrl: ['https://pics4.baidu.com/feed/d52a2834349b033b931245d4d14e0bdfd439bdb7.jpeg@f_auto?token=07cdf75443b8506e7a855325bed8d21a',
        "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F013fe45544a15e0000019ae9049657.jpg%401280w_1l_2o_100sh.jpg&refer=http%3A%2F%2Fimg.zcool.cn&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1663827624&t=884f3f882f3070c1168c89864ae1c289",
        'https://pics7.baidu.com/feed/43a7d933c895d14300d90fb057cfbe0e5baf0745.jpeg@f_auto?token=b95d13332f937dc8fdc0bfdc34eaec87']

    }
  },
  methods: {
    slideChange () {
      let activeIndex = this.$refs.mySwiper.swiper.activeIndex
      let realIndex = this.$refs.mySwiper.swiper.realIndex
      console.log('activeIndex', activeIndex)
      console.log('realIndex', realIndex)
      this.id = realIndex + 1
    },
    setIndex (i) {
      console.log('setIndex', i - 1)
      this.id = i - 1
      this.$refs.mySwiper.swiper.slideToLoop(i - 1, 1000, false)
    }
  }
}
</script>


<style >
.ditBox {
  width: 100%;
  display: flex;
  justify-content: center;
}
.dit {
  margin-right: 20px;
  width: 10px;
  height: 10px;
  background: #ccc;
  transition: all 10s;
}
.actived {
  width: 20px;
  background: #d70c0c;
}
.swiper-pagination-customs {
  display: inline-block;
  width: 5px;
  height: 5px;
  background: red;
  margin-right: 10px;
  transition: all 1s;
}
.swiper-pagination-customs-active {
  width: 20px;
  background: #fff;
}
</style>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值