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 lang="less">
.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')

第四步:在组件中直接使用轮播图组件

<Lunbotu :imgs="imgs" :autoplay="true" :duration="2"></Lunbotu>
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值