温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

vue-awesome-swiper怎么在VUE中使用

发布时间:2021-01-04 14:23:29 来源:亿速云 阅读:197 作者:Leah 栏目:开发技术

这篇文章给大家介绍vue-awesome-swiper怎么在VUE中使用,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。

一:首先进入项目目录中安装

install vue-awesome-swiper@2.6.7 --save

二.使用

全局挂载:

import Vue from 'vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
// require styles
import 'swiper/dist/css/swiper.css'

Vue.use(VueAwesomeSwiper, /* { default global options } */)

组件挂载

// require styles
import 'swiper/dist/css/swiper.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
 components: {
 swiper,
 swiperSlide
 }
}

新建一个.vue文件,按照以下代码模式

<template>
 <div class="index">
 <Top navLeft="true" title="轮播图" navRight="false"></Top>
  <div >
   <swiper id="mySwiper" :options="swiperOption" ref="mySwiper" v-if="swiperList.length!=0">
   <swiper-slide class="swiper-item" v-for='(item,index) of swiperList' :key='item.id' >
     <img class='swiper-img' :src='item.imgUrl' alt="门票" @click="swiperClick(index,item.linkUrl)" />
   </swiper-slide>
   <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
 </div>
 </div>
</template>
<script>
import Top from '@/components/public/Top';
import 'swiper/dist/css/swiper.css';
import { swiper, swiperSlide } from 'vue-awesome-swiper'
export default {
 name: 'Swiper',
 components: {Top,swiper,swiperSlide},
 data() {
  return {
  swiperList:[],
  swiperOption: {
   pagination: ".swiper-pagination",
   initialSlide: 0,//默认第几张
   loop:true, //循环
   autoplayDisableOnInteraction:false,//触摸后再次自动轮播
   autoplay:2000, //每张播放时长3秒,自动播放
   speed:1000,//滑动速度
  }
  }
 },
 created(){
 this.initEvent(); 
 console.log(this.$refs.mySwiper);
 this.swiperOption.autoplay = this.swiperList.length != 1 ? 2000 : false;//如果是一张图片不轮播

 
 },
 
 computed: {
  swiper() {
   return this.$refs.mySwiper.swiper//组件实例
  }
 },
 mounted(){ 
  
 },
 methods: {
 initEvent:function(){
  this.$http.get("http://localhost/swiper")
  .then(res=>{
   this.swiperList=res.data.swiperList;
  })
  .catch(error=>{
   console.log(error)
  })
 },
 swiperClick:function(index,url){
  console.log(index);
  this.$router.push(url)
 }
 }
}
</script>

<style scoped>
@import "../assets/public/css/bottom.css";
@import "../assets/css/index/my.css";
#mySwiper >>> .swiper-pagination-bullet {
 background: #000000;
}
#mySwiper >>> .swiper-pagination-bullet-active {
 background: #ff0000;
}
</style>

页面展示如下

vue-awesome-swiper怎么在VUE中使用

三:在使用过程中遇到的问题

1.触摸后再次自动轮播问题,添加以下属性就可以再次轮播

autoplayDisableOnInteraction:false

2.样式穿透问题,修改圆点的样式问题

  解决方案是给swiper容器添加上ID,然后再在后面添加>>>,这样就可以解决了

#mySwiper >>> .swiper-pagination-bullet {
 background: #000000;
}
#mySwiper >>> .swiper-pagination-bullet-active {
 background: #ff0000;
}

3.解决如果只有一张图片不轮播问题

关于vue-awesome-swiper怎么在VUE中使用就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI