1. 程式人生 > 程式設計 >如何在VUE中使用vue-awesome-swiper

如何在VUE中使用vue-awesome-swiper

一:首先進入專案目錄中安裝

install [email protected] --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 style="padding-top: 1.3rem;padding-left:0.2rem">
   <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中使用vue-awesome-swiper

三:在使用過程中遇到的問題

1.觸控後再次自動輪播問題,新增以下屬性就可以再次輪播

autoplayDisableOnInteraction:false

2.樣式穿透問題,修改圓點的樣式問題

解決方案是給swiper容器新增上ID,然後再在後面新增>>>,這樣就可以解決了

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

3.解決如果只有一張圖片不輪播問題

以上就是如何在VUE中使用vue-awesome-swiper的詳細內容,更多關於VUE中使用vue-awesome-swiper的資料請關注我們其它相關文章!