vue項(xiàng)目中使用swiper

由于項(xiàng)目中要求有左右滑動(dòng)切換賬號(hào)信息的需求,所以思考過(guò)后決定用swiper插件實(shí)現(xiàn)這個(gè)交互。
swiper文檔地址:https://www.swiper.com.cn/usage/index.html
一 、安裝

npm install swiper --save-dev

二、在使用到的頁(yè)面引入

<template>
<div class="swiper-container">
            <div class="swiper-wrapper">
              <div
                v-for="(el,index) in arrItem"
                :key="index"
                class="swiper-slide common_flex"
              >
                <img
                  class="img"
                  v-if="el.avatarUrl"
                  :src="el.avatarUrl"
                >
                <div
                  class="swiper-slide-name"
                  v-show="el.relation"
                >{{el.relation}}</div>
              </div>
            </div>
 </div>
</template>
<script>
import Swiper from "swiper";
import "swiper/swiper-bundle.css";
const c_swiper = document.getElementsByClassName("swiper-container");
mounted() {
    new Swiper(".swiper-container", {
      loop: false, // 循環(huán)模式選項(xiàng)
      // 如果需要分頁(yè)器
      pagination: {
        el: ".swiper-pagination",
      },
      slidesPerView: 3,
      centeredSlides: true,
      observer: true, //修改swiper自己或子元素時(shí),自動(dòng)初始化swiper
      observeParents: true, //修改swiper的父元素時(shí),自動(dòng)初始化swiper
      on: {
        //  2. 在滾動(dòng)事件中通過(guò)上面保存的swiper元素獲取當(dāng)前頁(yè)索引,可以打印一下c_swiper看一下內(nèi)部屬性
        slideChange: () => {
          console.log("輪播圖滾動(dòng)事件  --->", c_swiper[0].swiper.activeIndex);
          console.log("此處添加切換時(shí)要做的操作")
        },
      },
    });
  },
</script>

三、效果


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

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