简体中文
FxCarousel
约 1046 字大约 3 分钟
2025-12-16
在有限空间内循环播放同一类型的图片、文字等内容。
Carousel Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| height | 走马灯的高度 | string | — | — |
| initial-index | 初始状态激活的幻灯片的索引,从 0 开始 | number | — | 0 |
| trigger | 指示器的触发方式 | string | click | — |
| autoplay | 是否自动切换 | boolean | — | true |
| interval | 自动切换的时间间隔,单位为毫秒 | number | — | 3000 |
| indicator-position | 指示器的位置 | string | outside/none | — |
| arrow | 切换箭头的显示时机 | string | always/hover/never | hover |
| type | 走马灯的类型 | string | card | — |
| loop | 是否循环显示 | boolean | — | true |
| direction | 走马灯展示的方向 | string | horizontal/vertical | horizontal |
Carousel Events
| 事件名称 | 说明 | 回调参数 |
|---|---|---|
| change | 幻灯片切换时触发 | 目前激活的幻灯片的索引,原幻灯片的索引 |
Carousel Methods
| 方法名 | 说明 | 参数 |
|---|---|---|
| setActiveItem | 手动切换幻灯片 | 需要切换的幻灯片的索引,从 0 开始;或相应 el-carousel-item 的 name 属性值 |
| prev | 切换至上一张幻灯片 | — |
| next | 切换至下一张幻灯片 | — |
Carousel-Item Attributes
| 参数 | 说明 | 类型 | 可选值 | 默认值 |
|---|---|---|---|---|
| name | 幻灯片的名字,可用作 setActiveItem 的参数 | string | — | — |
| label | 该幻灯片所对应指示器的文本 | string | — | — |
基础用法
适用广泛的基础用法。
<template>
<div class="block">
<span class="demonstration">默认 Hover 指示器触发</span>
<fx-carousel height="150px">
<fx-carousel-item v-for="item in 4" :key="item">
<h3 class="small">{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</div>
<div class="block">
<span class="demonstration">Click 指示器触发</span>
<fx-carousel trigger="click" height="150px">
<fx-carousel-item v-for="item in 4" :key="item">
<h3 class="small">{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</div>
</template>
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 14px;
opacity: 0.75;
line-height: 150px;
margin: 0;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n+1) {
background-color: #d3dce6;
}
</style>指示器
可以将指示器的显示位置设置在容器外部。
<template>
<fx-carousel indicator-position="outside">
<fx-carousel-item v-for="item in 4" :key="item">
<h3>{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</template>
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 18px;
opacity: 0.75;
line-height: 300px;
margin: 0;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n+1) {
background-color: #d3dce6;
}
</style>切换箭头
可以设置切换箭头的显示时机。
<template>
<fx-carousel :interval="5000" arrow="always">
<fx-carousel-item v-for="item in 4" :key="item">
<h3>{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</template>
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 18px;
opacity: 0.75;
line-height: 300px;
margin: 0;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n+1) {
background-color: #d3dce6;
}
</style>卡片化
当页面宽度方向空间空余,但高度方向空间匮乏时,可使用卡片风格。
<template>
<fx-carousel :interval="4000" type="card" height="200px">
<fx-carousel-item v-for="item in 6" :key="item">
<h3 class="medium">{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</template>
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 14px;
opacity: 0.75;
line-height: 200px;
margin: 0;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n+1) {
background-color: #d3dce6;
}
</style>方向
默认情况下,direction 为 horizontal。通过设置 direction 为 vertical 来让走马灯在垂直方向上显示。
<template>
<fx-carousel height="200px" direction="vertical" :autoplay="false">
<fx-carousel-item v-for="item in 3" :key="item">
<h3 class="medium">{{ item }}</h3>
</fx-carousel-item>
</fx-carousel>
</template>
<style>
.el-carousel__item h3 {
color: #475669;
font-size: 14px;
opacity: 0.75;
line-height: 200px;
margin: 0;
}
.el-carousel__item:nth-child(2n) {
background-color: #99a9bf;
}
.el-carousel__item:nth-child(2n+1) {
background-color: #d3dce6;
}
</style>