天天看點

Vue 3.0 封裝輪播圖元件

第一步:封裝通用的輪播圖元件

<template>
  <!-- 輪播圖展示 -->
  <div class="xtx-carousel" @mouseleave="start" @mouseenter="stop">
    <ul class="carousel-body">
      <li v-for="(item, i) in imgs" :key="i" class="carousel-item" :class="{ fade: i === index }">
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <!-- 左右切換箭頭 -->
    <a href="javascript:;" class="carousel-btn prev" @click="toggle(-1)"><i class="iconfont icon-angle-left"></i></a>
    <a href="javascript:;" class="carousel-btn next" @click="toggle(1)"><i class="iconfont icon-angle-right"></i></a>
    <div class="carousel-indicator">
      <!-- 輪播圖小圓點 -->
      <span v-for="(item, i) in imgs" :key="i" :class="{ active: i === index }" @click="tog(i)"></span>
    </div>
  </div>
</template>

<script>
import { ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 父元件傳來得輪播圖資料
    imgs: {
      type: Array,
      default: () => {
        return []
      }
    },
    // 輪播圖自動播放屬性
    autoplay: {
      type: Boolean,
      default: true
    },
    // 播放間隔時間
    duration: { type: Number, default: 2000 }
  },
  setup(props) {
    // 預設顯示圖檔的索引
    const index = ref(0)
    let timer = null
    const autoplayFn = () => {
      clearInterval(timer)
      timer = setInterval(() => {
        index.value++
        if (index.value >= props.imgs.length) {
          index.value = 0
        }
      }, props.duration * 1000)
    }

    watch(
      () => props.imgs,
      newVal => {
        if (newVal.length > 1 && props.autoplay) {
          index.value = 0
          autoplayFn()
        }
      },
      { immediate: true }
    )
    // 滑鼠移出輪播圖開始播放
    const start = () => {
      if (props.imgs.length && props.autoplay) autoplayFn()
    }
    // 滑鼠移入輪播圖停止播放
    const stop = () => {
      if (timer) clearInterval(timer)
    }
    // 點選右側小箭頭;左側小箭頭切換圖檔
    const toggle = step => {
      index.value += step
      if (index.value >= props.imgs.length) {
        index.value = 0
        return
      }
      if (index.value < 0) {
        index.value = props.imgs.length - 1
      }
    }
    // 點選小圓點随機切換圖檔
    const tog = i => {
      index.value = i
    }
    return { index, start, stop, toggle, tog }
  }
}
</script>
           
<style scoped >
.xtx-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,0,0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background:  #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0,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>

           

第二步:全局注冊輪播圖元件,在components檔案夾index.js檔案中

import  Lunbotu from './xtx-carousel.vue'

export default {
  install (app) {
  
  app.component(Lunbotu.name,Lunbotu )
  }
}
           

第三步:在main.js中,注入自己封裝好的元件庫

import MyUI from '@/components/'
createApp(App)
  .use(store)
  .use(router)
  .use(MyUI)
  .mount('#app')
           

第四步:在元件中直接使用輪播圖元件