我开源了有关vue3的走马灯的组件

144 阅读2分钟

开源的起因

开源一个组件是十分需要想象力,但是如果在业务的加持下,这种想象力就能被打开了.而我最近便遇到了这种场景(在element-ui无法提供组件能力的情况下),我根据它的实现思路自己实现了一个更加强大的组件(自恋了哈哈哈哈哈).

实现思路

就是使用csstransform的特性,为子项位于统一水平线上,点击下一个按钮或者上一个按钮,移动的距离就是一个子项的宽度.在卡片模式下,利用了定位的层级关系,让激活的卡片永远在其它卡片之上.

使用

安装

npm install v-carousel-lucky --save

全局引入


import { createApp } from 'vue'
import App from './App.vue'
//引入css文件
import "v-carousel-lucky/dist/style.css"
// 引入组件
import vCarouselLucky from 'v-carousel-lucky'
const app = createApp(App);
app.use(vCarouselLucky)
app.mount("#app")

局部引入

import { Carousel, CarouselItem } from 'v-carousel-lucky'
import "v-carousel-lucky/dist/style.css"

使用


<script setup>
import { Carousel, CarouselItem } from 'v-carousel-lucky'
import "v-carousel-lucky/dist/style.css"

function beforeMoving(before) {
  console.log("before", before);
}
function afterMoving(after) {
  console.log("after", after);
}
</script>

<template>
  <div class="app">
    <carousel 
    :autoplay="true" 
    :duration="5000" 
    :initIndex="0" 
    :direction="true" 
    directionMode="hover" 
    :indicator="true"
    indicatorMode="always" 
    :loop="true" 
    type="card"  
    :delay="500"
    @before-move="beforeMoving" 
    @change-move="afterMoving">
      <carousel-item v-for="item in 3" :key="item" class="item">
        <div class="h">{{ item }}</div>
      </carousel-item>
    </carousel>
  </div>
</template>

<style scoped>
.app {
  width: 500px;
  height: 300px;
}

.h,
.item {
  width: 100%;
  height: 100%;
}

.item:nth-child(1) .h {
  background-color: rgb(198, 121, 22);
}

.item:nth-child(2) .h {
  background-color: rgb(74, 56, 33);
}

.item:nth-child(3) .h {
  background-color: rgb(39, 199, 68);
}
</style>

这样便能得到如下结果:

image.png

配置项如下:

名称介绍可选值默认值
width轮播的宽度-400px
height轮播的高度(不包含指示灯的高度)-300px
duration轮播延迟的时间-3000ms
autoplay是否自动轮播true/falsetrue
initIndex初始化展示的item-0(第一个)
direction是否展示切换按钮true/falsetrue
directionMode切换按钮展示的方式always/hoveralways
indicator指示灯是否展示true/falsetrue
indicatorMode指示灯展示方式always/hoveralways
indicatorPosition指示灯在外部还是内部inside/outsideinside
indicatorTrigger指示灯触发方式hover/clickhover
typecarousel的类型card-
loop是否循环轮播true/falsetrue
delay切换过渡时的事件-300
scale幻灯片缩小的比例小于10.8
cardWidth激活幻灯片的宽度小于整体宽度50%

Event

名称介绍参数
before-move目前激活幻灯片的前一个Object=>(index,direction)
change-move目前激活的幻灯片Object=>(index,direction)

结尾

欢迎大家star.哈哈哈哈哈哈.

GitHub