1. 程式人生 > 程式設計 >vue3封裝輪播圖元件的方法

vue3封裝輪播圖元件的方法

目的

封裝輪播圖元件,直接使用,具體內容如下

大致步驟

  • 準備my-carousel元件基礎佈局,全域性註冊
  • 準備home-banner元件,使用my-carousel元件,再首頁註冊使用。
  • 深度作用選擇器覆蓋my-carousel元件的預設樣式
  • 在home-banner元件獲取輪播圖資料,傳遞給my-carousel元件
  • 在my-carousel元件完成渲染
  • 自動播放,暴露自動輪播屬性,設定了就自動輪播
  • 如果有自動播放,滑鼠進入離開,暫停,開啟
  • 指示器切換,上一張,下一張
  • 銷燬元件,清理定時器

落地程式碼

一、封裝元件

<template>
  <div class="my-carousel" @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li v-for="(item,i) in findBannerList" :key="item.id" class="carousel-item" :class="{ fade: index === i }">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="圖片" />
        </RouterLink>
      </li>
    </ul>
    <a @click="clickFn(-1)" href=":;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click="clickFn(1)" href="script:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <span @click="active(i)" v-for="(item,i) in findBannerList" :key="i" :class="{ active: index === i }"></span>
    </div>
  </div>
</template>

<script>
import { onUnmounted,ref,watch } from ''
export default {
  name: 'Carousel',props: {
    findBannerList: {
      type: Array,default: () => []
    },autoplay: {
      type: Boolean,default: true
    },www.cppcns.com
duration: { type: Number,default: 3 } },setup(props) { const index = ref(0) // 定義一個常量儲存定時器 const timer = ref(null) // 定時器方法,實現自動輪播效果 const autoplayFn = () => { // 防抖,防止多次觸發定時器 clearInterval(timer.value) timer.value = setInterval(() => { index.value += 1 if (index.value >= props.findBannerList.length) { index.value = 0 } },props.duration * 1000) } // 偵聽器,根據介面返回的資料與傳遞的相關屬性引數 autoplay 開啟輪播 // 監聽返回的資料的長度,當長度大於1的時候並且 autoplay 的為 true 的時候開啟輪播 watch( () => props.findBannerList,() => { if (props.findBannerList.length > 1 && props.autoplay) { autoplayFn() } } ) // 滑鼠移入輪播圖,停止自動播放 const stop = () => { if (timer.value) clearInterval(timer.value) } // 滑鼠移出輪播圖,開啟定時器 const start = () => { if (props.findBannerList.length > 1 && props.autoplay) { autoplayFn() } } // 點選輪播圖上的左右按鈕,切換輪播圖,通過傳遞進來的引數,決定輪播圖往左往右 const clickFn = e => { index.value += e if (index.value >= props.findBannerList.length) { index.value = 0 } if (index.value < 0) { index.value = props.findBannerList.length - 1 } } // 點選指示器(輪播圖底下的小點)切換輪播圖 const active = e => { index.value = e } // 元件銷燬的時候情書定時器,避免效能損耗 onUnmounted(() => { if (timer.value) clearInterval(timer.value) }) return { index,stop,http://www.cppcns.com
start,clickFn,active } } } </script> <style scoped lang="less"> .my-carousel { width: 100%; height: 100%; min-width: 300px; min-height: 150px; position: relative; .carousel { &-body { width: 100%; height: 100%; } &-item { width: 100%; height: 100%; position: absolute; left: 0; top: 0; opacity: 0; transition: opacity 0.5s linear; &.fade { opacity: 1; z-index: 1; } img { width: 100%; height: 100%; } } &-indicator { position: absolute; left: 0; bottom: 20px; z-index: 2; width: 100%; text-align: center; span { display: inline-block; width: 12px; height: 12px; background: rgba(0,0.2); border-radius: 50%; cursor: pointer; ~ span { http://www.cppcns.com margin-left: 12px; } &.active { background: #fff; } } } &-btn { width: 44px; heiwww.cppcns.comght: 44px; background: rgba(0,0.2); color: #fff; border-radius: 50%; position: absolute; top: 228px; z-index: 2; text-align: center; line-height: 44px; opacity: 0; transition: all 0.5s; &.prev { left: 20px; } &.next { right: 20px; } } } &:hover { .carousel-btn { opacity: 1; } } } </style>

二、封裝成外掛

import MyCarousel from './my-carousel.vue'
export default {
  install(app) {
    app.component(MyCarousel.http://www.cppcns.comname,MyCarousel)
  }
}

三、在入口檔案 main. 中全域性註冊

import { createApp } from 'vue'
import App from './App.vue'
import MyUI from './components/library'

// 外掛的使用,在main.js使用app.use(外掛)
createApp(App).use(MyUI).mount('#app')

四、在專案中使用元件

準備home-banner元件,使用my-carousel元件,然後在專案中使用輪播的地方引入 home-banner 元件, 下面的引數可以在 home-banner 元件中設定

findBannerList 引數作為,後臺請求資料給到元件內部

autoplay 引數是否開啟輪播,預設 true 開啟輪播

duration 引數輪播停留時間間隔以 秒 為單位

<template>
  <div class="home-banner">
    <MyCarousel :findBannerList="findBannerList" :autoplay="true" :duration="3" />
  </div>
</template>

總結

按照思路步驟,一步步實現即可。

1.基本元件拆分和佈局
2.自動輪播
3.懸停控制啟動和停止
4.手動控制切換
5.銷燬定時器
6.抽取相關的引數

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。