vue + ts 如何封装轮播图组件

11 篇文章 0 订阅

功能

控制是否自动轮播,当鼠标悬停的时候停止自动轮播,鼠标离开继续自动轮播;可以设置轮播时长
在这里插入图片描述

1. 轮播图组件
<script lang="ts" setup name="GlCarousel">
export type slideItem = {
  id: string
  imgUrl: string
  hrefUrl: string
  type: string
}

import { ref, PropType, Ref, onMounted, onUnmounted } from 'vue'
const props = defineProps({
  slides: {
    type: Array as PropType<slideItem[]>,
    required: true
  },
  duration: {
    type: Number,
    default: 3000
  },
  autoPlay: {
    type: Boolean,
    default: false
  }
})

let active = ref(0)
let timer: Ref<any> = ref(null)

const prev = () => {
  active.value--
  if (active.value < 0) {
    active.value = props.slides.length - 1
  }
}

const next = () => {
  active.value++
  if (active.value >= props.slides.length) {
    active.value = 0
  }
}

const play = () => {
  if (!props.autoPlay) return
  // 防止开启多个定时器
  clearInterval(timer.value)
  timer.value = setInterval(next, props.duration)
}

const stop = () => {
  timer && clearInterval(timer.value)
}

onMounted(() => {
  play()
})

onUnmounted(() => {
  stop()
})
</script>

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

<style scoped lang="less">
.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>
2. 注册成全局组件
import glCarousel from './carousel/index.vue'
import { App } from 'vue'

const components = [glCarousel]

export default {
  install(app: App) {
    components.forEach((component) => {
      app.component(component.name, component)
    })
  },
}
3. 使用

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Vue3的Composition API来实现自动轮播图片并有切换效果的功能。下面是一个基本的示例: ``` <template> <div class="slider"> <div class="slider-container"> <div class="slider-item" v-for="(item, index) in images" :key="index" :style="{ backgroundImage: `url(${item})`, left: `${(index - currentIndex) * 100}%` }"></div> </div> </div> </template> <script lang="ts"> import { ref, onMounted, onUnmounted } from 'vue'; export default { setup() { const images = [ 'https://example.com/image1.jpg', 'https://example.com/image2.jpg', 'https://example.com/image3.jpg' ]; const currentIndex = ref(0); let interval: any = null; const startAutoSlide = () => { interval = setInterval(() => { currentIndex.value = (currentIndex.value + 1) % images.length; }, 3000); }; const stopAutoSlide = () => { clearInterval(interval); }; onMounted(() => { startAutoSlide(); }); onUnmounted(() => { stopAutoSlide(); }); return { images, currentIndex, startAutoSlide, stopAutoSlide }; } }; </script> <style scoped> .slider { width: 100%; overflow: hidden; position: relative; } .slider-container { width: 300%; display: flex; transition: left 0.5s ease-in-out; } .slider-item { width: 33.3333%; height: 200px; background-size: cover; background-position: center; position: absolute; top: 0; left: 0; } </style> ``` 在这个示例中,我们使用了一个`currentIndex`的ref来保存当前显示图片的索引。使用`setInterval`函数来定期更新`currentIndex`的值,达到自动轮播的效果。同时,我们将图片放置在一个横向滚动的容器中,使用`left`样式属性来控制当前显示的图片位置。 你可以根据自己的需要修改样式和定时器的时间间隔来实现更自定义的效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值