Vue封裝Swiper實(shí)現(xiàn)圖片輪播

圖片輪播是前端中經(jīng)常需要實(shí)現(xiàn)的一個(gè)功能。最近學(xué)習(xí)Vue.js,就針對Swiper進(jìn)行封裝,實(shí)現(xiàn)一個(gè)簡單的圖片輪播組件。

一、Swiper

在實(shí)現(xiàn)封裝之前,先介紹一下Swiper。

  • Swiper是純Javascript打造的滑動特效插件,面向手機(jī)、平板電腦等移動終端。

  • Swiper能實(shí)現(xiàn)觸屏焦點(diǎn)圖、觸屏Tab切換、觸屏多圖切換等常用效果。

  • Swiper開源、免費(fèi)、穩(wěn)定、使用簡單、功能強(qiáng)大,是架構(gòu)移動終端網(wǎng)站的重要選擇。

Swiper的應(yīng)用場景廣泛,實(shí)現(xiàn)效果很好,下面?zhèn)€這實(shí)際案例就是Swiper的典型應(yīng)用場景。


Swiper的具體使用教程及詳細(xì)API,參考Swiper中文網(wǎng)。

二、Vue組件

Vue組件設(shè)計(jì)初衷就是要配合使用的,提高維護(hù)性和復(fù)用性。而圖片輪播正適合使用組件來完成,因此在介紹具體的實(shí)現(xiàn)之前,先介紹下關(guān)于Vue組件及組件通信。

Vue組件中最常見的就是形成父子組件的關(guān)系:組件 A 在它的模板中使用了組件 B。

它們之間必然需要相互通信:父組件可能要給子組件下發(fā)數(shù)據(jù),子組件則可能要將它內(nèi)部發(fā)生的事情告知父組件。然而,通過一個(gè)良好定義的接口來盡可能將父子組件解耦也是很重要的。這保證了每個(gè)組件的代碼可以在相對隔離的環(huán)境中書寫和理解,從而提高了其可維護(hù)性和復(fù)用性。

在 Vue 中,父子組件的關(guān)系可以總結(jié)為 prop 向下傳遞,事件向上傳遞。父組件通過 prop 給子組件下發(fā)數(shù)據(jù),子組件通過事件給父組件發(fā)送消息。


三、封裝實(shí)現(xiàn)

1.引入Swiper

首先,需要安裝Swiper。

npm install --save swiper

然后,要引用兩個(gè)文件。

import Swiper from "swiper";
import "swiper/dist/css/swiper.min.css";

2.HTML代碼

在模板中設(shè)置輪播圖的html布局。

<template>
  <div class="swiper-container" :class="swipeid">
      <div class="swiper-wrapper">
          <!-- 存放具體的輪播內(nèi)容 -->
          <slot name ="swiper-con"></slot>
      </div>
      <!-- 分頁器 -->
      <div :class="{'swiper-pagination':pagination}"></div>
  </div>
</template>

其中使用具名插槽,提高解耦,使得在父組件使用時(shí),根據(jù)不同情況,設(shè)置不同的輪播內(nèi)容。

另外需要設(shè)置分頁器,即圖片輪播中的頁面指示器,常見的如小圓點(diǎn),或者數(shù)字指示器。

3.初始化Swiper

既然是對Swiper進(jìn)行封裝實(shí)現(xiàn)輪播圖,前面也已經(jīng)安裝了Swiper,那么現(xiàn)在就需要初始化使用。

在初始化之前,根據(jù)Swiper用法的了解,先確定輪播組件需要的屬性信息,然后通過父組件傳遞給封裝的Swiper組件。

這時(shí)候就需要用到props。

props: {
    swipeid: {
      type: String,
      default: ""
    },
    effect: {
      type: String,
      default: "slide"
    },
    loop: {
      type: Boolean,
      default: false
    },
    direction: {
      type: String,
      default: "horizontal"
    },
    pagination: {
      type: Boolean,
      default: true
    },
    paginationType: {
      type: String,
      default: "bullets"
    },
    autoPlay: {
      type: Number,
      default: 3000
    }
  }

下面逐一解釋每個(gè)屬性的含義。

屬性 含義
swiped 輪播容器class屬性的類名。
effect 圖片的 切換效果,默認(rèn)為"slide",還可設(shè)置為"fade", "cube", "coverflow","flip",詳情見effect。
loop 設(shè)置為true 則開啟loop模式。loop模式:會在原本圖片前后復(fù)制若干個(gè)圖片并在合適的時(shí)候切換,讓Swiper看起來是循環(huán)的,詳情見loop。
direction 圖片的滑動方向,可設(shè)置水平(horizontal)或垂直(vertical),詳情見direction。
pagination 使用分頁導(dǎo)航,詳情見pagination。
paginationType 分頁器樣式類型,可設(shè)置為"bullets", "fraction", "progressbar", "custom",詳情見type
autoPlay 設(shè)置為true啟動自動切換,并使用默認(rèn)的切換設(shè)置,詳情見autoplay。

了解了上面每個(gè)屬性的含義,下面就可以初始化Swiper,并設(shè)置具體的屬性。

初始化Swiper時(shí),需要傳入兩個(gè)參數(shù)。

  • 輪播容器的類名
  • 代表圖片輪播組件詳細(xì)功能的對象
    var that = this;
    this.dom = new Swiper("." + that.swipeid, {
      //循環(huán)
      loop: that.loop,
      //分頁器
      pagination: { 
        el: ".swiper-pagination",
        bulletClass : 'swiper-pagination-bullet',
            },
      //分頁類型
      paginationType: that.paginationType,
      //自動播放
      autoPlay: that.autoPlay,
      //方向
      direction: that.direction,
      //特效
      effect: that.effect,
      //用戶操作swiper之后,不禁止autoplay
      disableOnInteraction: false,
      //修改swiper自己或子元素時(shí),自動初始化swiper
      observer: true,
      //修改swiper的父元素時(shí),自動初始化swiper
      observeParents: true
    });
  }

四、自定義輪播效果

經(jīng)過上面的步驟,輪播器就封裝好了。我們可以自定義實(shí)現(xiàn)自己想要的輪播器效果。下面以知乎的API為例,實(shí)現(xiàn)圖片輪播。

1.HTML代碼

<m-swipe swipeid="swipe" ref="swiper" :autoPlay="3000" effect="slide">
      <div v-for="top in tops" :key="top.id" class="swiper-slide" slot="swiper-con" >
        <img :src="top.image">
        <h3>{{top.title}}</h3>
      </div>
</m-swipe>

首先要引用注冊組件,這里就不詳細(xì)寫出。

其中m-swipe就是前面實(shí)現(xiàn)的圖片輪播組件,而其中的子組件就是通過具名插槽插入的輪播內(nèi)容。

2.CSS代碼

.swiper-container {
    width: 100%;
  }
  .swiper-slide {
    height: 8rem;
    overflow: hidden;
    position: relative;
  }
.swiper-slide {
  div {
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0.4;
    position: absolute;
    background-color: @blue;
  }
  img {
    top: 50%;
    position: relative;
    transform: translate(0, -50%);
  }
  h3 {
    width: 70%;
    color: #fff;
    margin: 0;
    font-size: 0.5rem;
    line-height: 1rem;
    right: 5%;
    bottom: 2.6rem;
    text-align: right;
    position: absolute;
    text-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
    &:before {
      content: "";
      width: 3rem;
      bottom: -0.6rem;
      right: 0;
      display: block;
      position: absolute;
      border: 2px solid @yellow;
    }
  }
}
.swiper-pagination-bullet-active {
  background: #fff;
}
.swiper-container-horizontal > .swiper-pagination-bullets {
    bottom: 1rem;
    width: 95%;
    text-align: right;
  }

其中swiper-pagination-bullet-active代表分頁器中當(dāng)前指示的小圓點(diǎn)的類名。swiper-pagination-bullets代表分頁器的類名,詳情見pagination分頁器內(nèi)元素的類名。

關(guān)于網(wǎng)絡(luò)請求數(shù)據(jù)展示的代碼就不貼了,下面有源碼地址。

3.效果

這只是一個(gè)簡單的封裝效果,想要實(shí)現(xiàn)更多的效果,可以通過Swiper中提供的更多功能來實(shí)現(xiàn)。

Github地址: 圖片輪播

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容