Appearance
Carousel 走马灯
旋转木马,一组轮播的区域。
代码演示
基础用法
vue
<template>
<a-carousel :after-change="onChange">
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<script setup>
const onChange = current => {
console.log(current);
};
</script>
<style scoped>
/* For demo */
:deep(.slick-slide) {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
:deep(.slick-slide h3) {
color: #fff;
}
</style>位置
位置有 4 个方向。
vue
<template>
<a-radio-group v-model:value="dotPosition" style="margin-bottom: 8px">
<a-radio-button value="top">Top</a-radio-button>
<a-radio-button value="bottom">Bottom</a-radio-button>
<a-radio-button value="left">Left</a-radio-button>
<a-radio-button value="right">Right</a-radio-button>
</a-radio-group>
<a-carousel :dot-position="dotPosition">
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<script setup>
import { ref } from 'vue';
const dotPosition = ref('top');
</script>
<style scoped>
/* For demo */
:deep(.slick-slide) {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
:deep(.slick-slide h3) {
color: #fff;
}
</style>渐显
切换效果为渐显。
vue
<template>
<a-carousel effect="fade">
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<style scoped>
/* For demo */
:deep(.slick-slide) {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
:deep(.slick-slide h3) {
color: #fff;
}
</style>自动切换
定时切换下一张。
vue
<template>
<a-carousel autoplay>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<style scoped>
/* For demo */
:deep(.slick-slide) {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
:deep(.slick-slide h3) {
color: #fff;
}
</style>自定义分页
自定义分页展示。
vue
<template>
<a-carousel arrows dots-class="slick-dots slick-thumb">
<template #customPaging="props">
<a>
<img :src="getImgUrl(props.i)" />
</a>
</template>
<div v-for="item in 4" :key="item">
<img :src="getImgUrl(item - 1)" />
</div>
</a-carousel>
</template>
<script setup>
import { defineComponent } from 'vue';
const baseUrl =
'https://raw.githubusercontent.com/vueComponent/ant-design-vue/main/components/carousel/demo/';
export default defineComponent({
setup() {
const getImgUrl = i => {
return `${baseUrl}abstract0${i + 1}.jpg`;
};
return {
getImgUrl,
};
},
});
</script>
<style scoped>
/* For demo */
:deep(.slick-dots) {
position: relative;
height: auto;
}
:deep(.slick-slide img) {
border: 5px solid #fff;
display: block;
margin: auto;
max-width: 80%;
}
:deep(.slick-arrow) {
display: none !important;
}
:deep(.slick-thumb) {
bottom: 0px;
}
:deep(.slick-thumb li) {
width: 60px;
height: 45px;
}
:deep(.slick-thumb li img) {
width: 100%;
height: 100%;
filter: grayscale(100%);
display: block;
}
:deep .slick-thumb li.slick-active img {
filter: grayscale(0%);
}
</style>自定义箭头
自定义箭头展示。
vue
<template>
<a-carousel arrows>
<template #prevArrow>
<div class="custom-slick-arrow" style="left: 10px; z-index: 1">
</div>
</template>
<template #nextArrow>
<div class="custom-slick-arrow" style="right: 10px">
</div>
</template>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<script setup>
export {};
</script>
<style scoped>
/* For demo */
:deep(.slick-slide) {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
:deep(.slick-arrow.custom-slick-arrow) {
width: 25px;
height: 25px;
font-size: 25px;
color: #fff;
background-color: rgba(31, 45, 61, 0.11);
transition: ease all 0.3s;
opacity: 0.3;
z-index: 1;
}
:deep(.slick-arrow.custom-slick-arrow:before) {
display: none;
}
:deep(.slick-arrow.custom-slick-arrow:hover) {
color: #fff;
opacity: 0.5;
}
:deep(.slick-slide h3) {
color: #fff;
}
</style>API
| 参数 | 说明 | 类型 | 默认值 |
|---|---|---|---|
| autoplay | 是否自动切换 | boolean | false |
| dotPosition | 面板指示点位置,可选 top bottom left right | string | bottom |
| dots | 是否显示面板指示点 | boolean | true |
| dotsClass | 面板指示点类名 | string | slick-dots |
| easing | 动画效果 | string | linear |
| effect | 动画效果函数 | scrollx | fade | scrollx |
| afterChange | 切换面板的回调 | function(current) | - |
| beforeChange | 切换面板的回调 | function(from, to) | - |
方法
| 名称 | 描述 |
|---|---|
| goTo(slideNumber, dontAnimate) | 切换到指定面板, dontAnimate = true 时,不使用动画 |
| next() | 切换到下一面板 |
| prev() | 切换到上一面板 |





