Carousel走马灯

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 lang="ts">import { defineComponent } from 'vue';export default defineComponent({  setup() {    const onChange = (current: number) => {      console.log(current);    };    return {      onChange,    };  },});<​/script><​style scoped>/* For demo */.ant-carousel :deep(.slick-slide) {  text-align: center;  height: 160px;  line-height: 160px;  background: #364d79;  overflow: hidden;}.ant-carousel :deep(.slick-slide h3) {  color: #fff;}<​/style>

位置

位置有 4 个方向。

<​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 lang="ts">import { defineComponent, ref } from 'vue';export default defineComponent({  setup() {    return {      dotPosition: ref('top'),    };  },});<​/script><​style scoped>/* For demo */.ant-carousel :deep(.slick-slide) {  text-align: center;  height: 160px;  line-height: 160px;  background: #364d79;  overflow: hidden;}.ant-carousel :deep(.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><​style scoped>/* For demo */.ant-carousel :deep(.slick-slide) {  text-align: center;  height: 160px;  line-height: 160px;  background: #364d79;  overflow: hidden;}.ant-carousel :deep(.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><​style scoped>/* For demo */.ant-carousel :deep(.slick-slide) {  text-align: center;  height: 160px;  line-height: 160px;  background: #364d79;  overflow: hidden;}.ant-carousel :deep(.slick-slide h3) {  color: #fff;}<​/style>

自定义分页

自定义分页展示。

<​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="baseUrl + 'abstract0' + item + '.jpg'" />    <​/div>  <​/a-carousel><​/template><​script lang="ts">import { defineComponent } from 'vue';const baseUrl =  'https://raw.githubusercontent.com/vueComponent/ant-design-vue/master/components/vc-slick/assets/img/react-slick/';export default defineComponent({  setup() {    const getImgUrl = (i: number) => {      return `${baseUrl}abstract0${i + 1}.jpg`;    };    return {      baseUrl,      getImgUrl,    };  },});<​/script><​style scoped>/* For demo */.ant-carousel :deep(.slick-dots) {  position: relative;  height: auto;}.ant-carousel :deep(.slick-slide img) {  border: 5px solid #fff;  display: block;  margin: auto;  max-width: 80%;}.ant-carousel :deep(.slick-arrow) {  display: none !important;}.ant-carousel :deep(.slick-thumb) {  bottom: 0px;}.ant-carousel :deep(.slick-thumb li) {  width: 60px;  height: 45px;}.ant-carousel :deep(.slick-thumb li img) {  width: 100%;  height: 100%;  filter: grayscale(100%);}.ant-carousel :deep .slick-thumb li.slick-active img {  filter: grayscale(0%);}<​/style>

自定义箭头

自定义箭头展示。

<​template>  <​a-carousel arrows>    <​template #prevArrow>      <​div class="custom-slick-arrow" style="left: 10px; zindex: 1">        <​left-circle-outlined />      <​/div>    <​/template>    <​template #nextArrow>      <​div class="custom-slick-arrow" style="right: 10px">        <​right-circle-outlined />      <​/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 lang="ts">import { LeftCircleOutlined, RightCircleOutlined } from '@ant-design/icons-vue';import { defineComponent } from 'vue';export default defineComponent({  components: {    LeftCircleOutlined,    RightCircleOutlined,  },});<​/script><​style scoped>/* For demo */.ant-carousel :deep(.slick-slide) {  text-align: center;  height: 160px;  line-height: 160px;  background: #364d79;  overflow: hidden;}.ant-carousel :deep(.slick-arrow.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 :deep(.custom-slick-arrow:before) {  display: none;}.ant-carousel :deep(.custom-slick-arrow:hover) {  opacity: 0.5;}.ant-carousel :deep(.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