阅读 593

vue 复用手写轮播图组件

按照需求对应的封装轮播图组件

需求暂定为:宽高,背景颜色,是否自动轮播,轮播时间间隔

封装组件:

<template>
  <div class="xtx-carousel" @mouseenter="enter" @mouseleave="leave">
    <ul class="carousel-body">
      <li
        class="carousel-item"
        :class="{ fade: curIdx === idx }"
        v-for="(item, idx) in sliders"
        :key="item.id"
      >
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="" />
        </RouterLink>
      </li>
    </ul>
    <a href="javascript:;" @click="toggle(-1)" class="carousel-btn prev"
      ><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="(i, idx) in 5"
        :key="i"
        @click="curIdx = idx"
        :class="{ active: idx === curIdx }"
      ></span>
    </div>
  </div>
</template>

<script>
import { onUnmounted, ref, watch } from 'vue'
export default {
  props: {
    sliders: {
      type: Array,
      default: () => []
    },
    autoplay: {
      type: Boolean,
      default: false
    },
    duration: {
      type: Number,
      default: 2000
    }
  },
  name: 'XtxCarousel',
  setup (props) {
    const curIdx = ref(0)

    // 自动播放
    let timer = null
    const autoplay = () => {
      timer = setInterval(() => {
        curIdx.value++
        if (curIdx.value >= props.sliders.length) curIdx.value = 0
      }, props.duration)
    }
    // sliders中有数据就自动播放
    watch(() => props.sliders, (newVal, oldVal) => {
      if (newVal.length > 0) {
        if (props.autoplay) {
          autoplay()
        }
      }
    }, { immediate: true })

    onUnmounted(() => {
      clearInterval(timer)
    })

    const leave = () => {
      if (props.autoplay) autoplay()
    }
    const enter = () => {
      clearInterval(timer)
    }
    // 切换上一张和下一张
    const toggle = step => {
      curIdx.value += step
      if (curIdx.value >= props.sliders.length) curIdx.value = 0
      if (curIdx.value < 0) curIdx.value = props.sliders.length - 1
    }
    return { curIdx, leave, enter, toggle }
  }
}
</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, 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>

复制代码

这里简要的对其说明一下:

sliders为从上一级组件传递过来的数据列表,格式为数组

autoplay为从上级组件传递过来的布尔类型的值,意思为是否开启自动轮播

duration从上级组件传递过来的时间间隔,意为多长时间轮播一次,类型为number

props: {
    sliders: {
      type: Array,
      default: () => []
    },
    autoplay: {
      type: Boolean,
      default: false
    },
    duration: {
      type: Number,
      default: 2000
    }
  },
复制代码

之后将组件定义传值即可使用,可以全局,也可局部定义,将这里将其定义到全局

在专门定义全局组件的js文件中:

//引入轮播图组件
import XtxCarousel from './xtx-carousel.vue'

const myPlugin = {
  install (app) {
    // app为vue的实例
    // app.component('组件名',组件对象)
    app.component(XtxCarousel.name, XtxCarousel)
  }
}

export default myPlugin
复制代码

在main.js中引入并use()即可

// 引入全局组件
import myPlugin from '@/components/index'

createApp(App).use(store).use(router).use(myPlugin).mount('#app')
复制代码

vue2,vue3引入全局组件的差异

文章分类
前端
文章标签