Carousel 走马灯
旋转木马,一组轮播的区域。
何时使用
基本
最简单的用法。
<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>
export default {
methods: {
onChange(a, b, c) {
console.log(a, b, c);
},
},
};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-slide {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
.ant-carousel >>> .slick-slide h3 {
color: #fff;
}
</style>
位置
位置有 4 个方向。
<template>
<div>
<a-radio-group v-model="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>
</div>
</template>
<script>
export default {
data() {
return {
dotPosition: 'top',
};
},
};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-slide {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
.ant-carousel >>> .slick-slide h3 {
color: #fff;
}
</style>
渐显
切换效果为渐显。
<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>
<script>
export default {};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-slide {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
.ant-carousel >>> .slick-slide h3 {
color: #fff;
}
</style>
自动切换
定时切换下一张。
<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>
<script>
export default {};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-slide {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
.ant-carousel >>> .slick-slide h3 {
color: #fff;
}
</style>
自定义分页
自定义分页展示。
<template>
<a-carousel arrows dots-class="slick-dots slick-thumb">
<a slot="customPaging" slot-scope="props">
<img :src="getImgUrl(props.i)" />
</a>
<div v-for="item in 4">
<img :src="baseUrl + 'abstract0' + item + '.jpg'" />
</div>
</a-carousel>
</template>
<script>
const baseUrl =
'https://raw.githubusercontent.com/vueComponent/ant-design-vue/master/components/vc-slick/assets/img/react-slick/';
export default {
data() {
return {
baseUrl,
};
},
methods: {
getImgUrl(i) {
return `${baseUrl}abstract0${i + 1}.jpg`;
},
},
};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-dots {
height: auto;
}
.ant-carousel >>> .slick-slide img {
border: 5px solid #fff;
display: block;
margin: auto;
max-width: 80%;
}
.ant-carousel >>> .slick-thumb {
bottom: -45px;
}
.ant-carousel >>> .slick-thumb li {
width: 60px;
height: 45px;
}
.ant-carousel >>> .slick-thumb li img {
width: 100%;
height: 100%;
filter: grayscale(100%);
}
.ant-carousel >>> .slick-thumb li.slick-active img {
filter: grayscale(0%);
}
</style>
自定义箭头
自定义箭头展示。
<template>
<a-carousel arrows>
<div
slot="prevArrow"
slot-scope="props"
class="custom-slick-arrow"
style="left: 10px;zIndex: 1"
>
<a-icon type="left-circle" />
</div>
<div slot="nextArrow" slot-scope="props" class="custom-slick-arrow" style="right: 10px">
<a-icon type="right-circle" />
</div>
<div><h3>1</h3></div>
<div><h3>2</h3></div>
<div><h3>3</h3></div>
<div><h3>4</h3></div>
</a-carousel>
</template>
<script>
export default {};
</script>
<style scoped>
/* For demo */
.ant-carousel >>> .slick-slide {
text-align: center;
height: 160px;
line-height: 160px;
background: #364d79;
overflow: hidden;
}
.ant-carousel >>> .custom-slick-arrow {
width: 25px;
height: 25px;
font-size: 25px;
color: #fff;
background-color: rgba(31, 45, 61, 0.11);
opacity: 0.3;
}
.ant-carousel >>> .custom-slick-arrow:before {
display: none;
}
.ant-carousel >>> .custom-slick-arrow:hover {
opacity: 0.5;
}
.ant-carousel >>> .slick-slide h3 {
color: #fff;
}
</style>
API
参数 | 说明 | 类型 | 默认值 | 版本 |
---|---|---|---|---|
afterChange | 切换面板的回调 | function(current) | 无 | |
autoplay | 是否自动切换 | boolean | false | |
beforeChange | 切换面板的回调 | function(from, to) | 无 | |
dotPosition | 面板指示点位置,可选 top bottom left right | string | bottom | 1.5.0 |
dots | 是否显示面板指示点 | boolean | true | |
dotsClass | 面板指示点类名 | string | slick-dots | |
easing | 动画效果 | string | linear | |
effect | 动画效果函数,可取 scrollx, fade | string | scrollx |
方法
名称 | 描述 | 版本 |
---|---|---|
goTo(slideNumber, dontAnimate) | 切换到指定面板, dontAnimate = true 时,不使用动画 | |
next() | 切换到下一面板 | |
prev() | 切换到上一面板 |
更多参数可参考:vc-slick props