vue使用swiper的简单用法

<template>

<div @mouseenter="on_top_enter" @mouseleave="on_top_leave">

<swiper :options="swiperOption" v-if="swiperSlides.length>1" ref="mySwiper">

    <swiper-slide v-for="(slide, index) in swiperSlides" :key="index">

      <img :src="slide.url">

    </swiper-slide>

    <div class="swiper-pagination" slot="pagination"></div>

  </swiper>

</div>

</template>

<script>

import 'swiper/dist/css/swiper.css'

import {swiper,swiperSlide} from 'vue-awesome-swiper'

import axios from "axios"

export default {

  name: "Index",

  data() {

    return {

      swiperSlides: [],

      swiperOption:{

          pagination:{

            el:'.swiper-pagination',

            clickable:true,

          },

          // paginationClickable:true

          autoplay:{

            delay:5000,//秒

            stopOnLastSlide:false,

            disableOnInteraction:false,//滑动不会失效

          },

          // slidesPerView: 3,

          loop:true,//loop模式:会在原本slide前后复制若干个slide(默认一个)并在合适的时候切换,让Swiper看起来是循环的。

          loopFillGroupWithBlank: true,

          autoplayDisableOnInteraction:true

      },

      }

  },

  components:{

    swiper,

    swiperSlide

  },

  mounted(){

    this.loadBanner();

    // setInterval(()=>{

    //  console.log('异步数据')

    //  if(this.swiperSlides.length<3){

    //    this.swiperSlides.push(this.swiperSlides.length+1)

    //  }

    // },3000); 

  },

  methods: {

    loadBanner(){

      this.$http.get('../../static/data.json')

      .then((res)=>{

        console.log(res.data);

        this.swiperSlides=res.data.banner;

        console.log(this.swiperSlides);

      })

      .catch(function(res){

        console.log(res);

      })

    },

//通过获得的swiper对象来暂停自动播放

    on_top_enter(){

      this.mySwiper.autoplay.stop()

    },

    on_top_leave(){

      this.mySwiper.autoplay.start()

    }

  },

  computed:{

    //计算属性,获得可以操作的swiper对象

    mySwiper(){

      return this.$refs.mySwiper.swiper

    }

  }


};

</script>

<style scoped>

img{

  width: 100%;

  height: auto;

}

</style>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 姓名:徐伟 常州新日催化剂有限公司 组别:第455期乐观二组纪律委员 【日精进打卡第51天】 【知~学习】 书名:...
    奔波儿灞_87f6阅读 1,661评论 0 0
  • 亲子日记第122天 星期四 多云 早晨大宝上学的时候就叮嘱我,别忘了下午早点去,不会忘的,我怎么会忘记呢...
    吉祥_0486阅读 1,418评论 0 0
  • 刚上完补课班回到家,妈妈就高兴的把我拽过来告诉我,今晚咱们去万达看一场电影,它现在可有名气了,特别的搞笑,...
    过冬的喵星人阅读 1,666评论 0 1
  • 离开家人久了,对家人的关心也少了许多。周末也很少往家里打电话,也不知道接起电话的那一刻说些什么。自从上了大学,一个...
    每日情书阅读 1,670评论 0 0
  • 自我介绍本该谈谈:我是谁?我从哪来?我将到哪去?这里我就说说现在的我吧。 现在的我是个孕妈妈,孕期反应大。怀孕前,...
    心生明媚阅读 2,817评论 0 2

友情链接更多精彩内容