Carousel 走马灯

一组轮播的区域。

何时使用

  1. 用于展示图片或者卡片。

基本用法

<template>
  <d-carousel height="200px">
    <d-carousel-item class="d-carousel-item" v-for="item in items" :key="item">{{ item }}</d-carousel-item>
  </d-carousel>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const items = ref<string[]>(['page 1', 'page 2', 'page 3', 'page 4']);
    return {
      items,
    };
  },
});
</script>
<style>
.d-carousel-item {
  text-align: center;
  line-height: 200px;
  background: var(--devui-global-bg, #f3f6f8);
}
</style>

过渡时间

<template>
  <d-carousel height="200px" :transition-speed="5000">
    <d-carousel-item class="d-demo-carousel-item" v-for="item in items" :key="item">{{ item }}</d-carousel-item>
  </d-carousel>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const items = ref<string[]>(['page 1', 'page 2', 'page 3', 'page 4']);
    return {
      items,
    };
  },
});
</script>
<style>
.d-demo-carousel-item {
  text-align: center;
  line-height: 200px;
  background: var(--devui-global-bg, #f3f6f8);
}
</style>

指示器&切换箭头

arrowTrigger 设为 always 可以使箭头永久显示,dotTrigger 设为 hover 可以使 hover 到点上就切换。

<template>
  <d-carousel height="200px" arrowTrigger="always" dot-trigger="hover">
    <d-carousel-item class="d-carousel-item" v-for="item in items" :key="item">{{ item }}</d-carousel-item>
  </d-carousel>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const items = ref<string[]>(['page 1', 'page 2', 'page 3', 'page 4']);
    return {
      items,
    };
  },
});
</script>
<style>
.d-carousel-item {
  text-align: center;
  line-height: 200px;
  background: var(--devui-global-bg, #f3f6f8);
}
</style>

自动轮播

<template>
  <d-carousel height="200px" autoplay :autoplay-speed="3000">
    <d-carousel-item class="d-carousel-item" v-for="item in items" :key="item">{{ item }}</d-carousel-item>
  </d-carousel>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const items = ref<string[]>(['page 1', 'page 2', 'page 3', 'page 4']);
    return {
      items,
    };
  },
});
</script>
<style>
.d-carousel-item {
  text-align: center;
  line-height: 200px;
  background: var(--devui-global-bg, #f3f6f8);
}
</style>

自定义操作

<template>
  <d-carousel ref="carousel" height="200px" arrowTrigger="never">
    <d-carousel-item class="d-carousel-item" v-for="item in items" :key="item">{{ item }}</d-carousel-item>
  </d-carousel>
  <div class="carousel-demo-operate">
    <d-button @click="onPrev">上一张</d-button>
    <d-button @click="onNext">下一张</d-button>
    <d-button @click="onGoFirst">第一张</d-button>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue';

export default defineComponent({
  setup() {
    const items = ref<string[]>(['page 1', 'page 2', 'page 3', 'page 4']);
    const carousel = ref();
    const onPrev = () => {
      carousel.value?.prev?.();
    };
    const onNext = () => {
      carousel.value?.next?.();
    };
    const onGoFirst = () => {
      carousel.value?.goto?.(0);
    };
    return {
      items,
      carousel,
      onPrev,
      onNext,
      onGoFirst,
    };
  },
});
</script>
<style>
.carousel-demo-operate {
  margin-top: 10px;
  display: flex;
  align-items: center;
}
.carousel-demo-operate .devui-btn-host {
  margin-right: 20px;
}
</style>
参数类型默认值描述跳转 Demo
arrow-trigger'hover'|'never'|'always''hover'可选,指定切换箭头显示方式指示器&切换箭头
autoplaybooleanfalse可选,是否自动轮播自动轮播
autoplay-speednumber3000可选,配合autoplay使用,自动轮播速度,单位 ms自动轮播
heightstring'100%'可选,轮播容器高度基本用法
show-dotsbooleantrue可选,是否显示面板指示器自动轮播
dot-position'top'|'bottom''bottom'可选,面板指示器位置指示器&切换箭头
dot-trigger'click'|'hover''click'可选,指示器触发轮播方式指示器&切换箭头
active-indexnumber0可选,初始化激活卡片索引,从 0 开始,支持[(active-index)]双向绑定基本用法
transition-speednumber500可选,切换到下一张所用的时间,单位ms基本用法
事件类型描述跳转 Demo
active-index-changeEventEmitter<number>卡片切换时,返回当前卡片的索引,从 0 开始基本用法
方法描述跳转 Demo
prev()切换到上一张卡片自定义操作
next()切换到下一张卡片自定义操作
goTo(index)切换到指定索引的卡片,索引从 0 开始自定义操作
Contributors