Vue驿站前端Vue专辑

vue-awesome-swiper

2020-02-25  本文已影响0人  edisonTechBlog

今天做vue的轮播功能,然后遇到了很大的坑,首先是最新版的插件是基于swiper4.相比swiper5及其难用。导致弄了很久才排除大坑

最值得一提的是,在手动去化轮播图时,会报错:

最后在网上找到一个解决方案:去swiper的js文件中搜索 e.preventDefault() 然后将其屏蔽即可。

接下来说说在vue中如何使用。当然,具体的参考swiper4官方文档!

因为不止一个组件使用到iswiper,所以定义到全局

在组件中就直接粘贴代码了:

<!-- The ref attr used to find the swiper instance -->
<template>
  <swiper :options="swiperOption" ref="mySwiper" @someSwiperEvent="callback">
    <!-- slides -->
    <swiper-slide>I'm Slide 1</swiper-slide>
    <swiper-slide>I'm Slide 2</swiper-slide>
    <swiper-slide>I'm Slide 3</swiper-slide>
    <swiper-slide>I'm Slide 4</swiper-slide>
    <swiper-slide>I'm Slide 5</swiper-slide>
    <swiper-slide>I'm Slide 6</swiper-slide>
    <swiper-slide>I'm Slide 7</swiper-slide>
    <!-- Optional controls -->
    <div class="swiper-pagination"  slot="pagination"></div>
    <div class="swiper-button-prev" slot="button-prev"></div>
    <div class="swiper-button-next" slot="button-next"></div>
    <div class="swiper-scrollbar"   slot="scrollbar"></div>
  </swiper>
</template>

<script>
  export default {
    name: 'carrousel',
    data() {
      return {
        swiperOption: {
          // some swiper options/callbacks
          // 所有的参数同 swiper 官方 api 参数
          // ...
        }
      }
    },
    computed: {
      swiper() {
        return this.$refs.mySwiper.swiper
      }
    },
    mounted() {
      // current swiper instance
      // 然后你就可以使用当前上下文内的swiper对象去做你想做的事了
      console.log('this is current swiper instance object', this.swiper)
      this.swiper.slideTo(3, 1000, false)
    }
  }
</script>
上一篇下一篇

猜你喜欢

热点阅读