Carousel走马灯 - 图1

Carousel 走马灯

旋转木马,一组轮播的区域。

何时使用

  • 当有一组平级的内容。
  • 当内容空间不足时,可以用走马灯的形式进行收纳,进行轮播展现。
  • 常用于一组图片或卡片轮播。

代码演示

Carousel走马灯 - 图2

基本

最简单的用法。

  1. <template>
  2. <a-carousel :after-change="onChange">
  3. <div><h3>1</h3></div>
  4. <div><h3>2</h3></div>
  5. <div><h3>3</h3></div>
  6. <div><h3>4</h3></div>
  7. </a-carousel>
  8. </template>
  9. <script lang="ts">
  10. import { defineComponent } from 'vue';
  11. export default defineComponent({
  12. setup() {
  13. const onChange = (current: number) => {
  14. console.log(current);
  15. };
  16. return {
  17. onChange,
  18. };
  19. },
  20. });
  21. </script>
  22. <style scoped>
  23. /* For demo */
  24. .ant-carousel :deep(.slick-slide) {
  25. text-align: center;
  26. height: 160px;
  27. line-height: 160px;
  28. background: #364d79;
  29. overflow: hidden;
  30. }
  31. .ant-carousel :deep(.slick-slide h3) {
  32. color: #fff;
  33. }
  34. </style>

Carousel走马灯 - 图3

Carousel走马灯 - 图4

位置

位置有 4 个方向。

  1. <template>
  2. <a-radio-group v-model:value="dotPosition" style="margin-bottom: 8px">
  3. <a-radio-button value="top">Top</a-radio-button>
  4. <a-radio-button value="bottom">Bottom</a-radio-button>
  5. <a-radio-button value="left">Left</a-radio-button>
  6. <a-radio-button value="right">Right</a-radio-button>
  7. </a-radio-group>
  8. <a-carousel :dot-position="dotPosition">
  9. <div><h3>1</h3></div>
  10. <div><h3>2</h3></div>
  11. <div><h3>3</h3></div>
  12. <div><h3>4</h3></div>
  13. </a-carousel>
  14. </template>
  15. <script lang="ts">
  16. import { defineComponent, ref } from 'vue';
  17. export default defineComponent({
  18. setup() {
  19. return {
  20. dotPosition: ref('top'),
  21. };
  22. },
  23. });
  24. </script>
  25. <style scoped>
  26. /* For demo */
  27. .ant-carousel :deep(.slick-slide) {
  28. text-align: center;
  29. height: 160px;
  30. line-height: 160px;
  31. background: #364d79;
  32. overflow: hidden;
  33. }
  34. .ant-carousel :deep(.slick-slide h3) {
  35. color: #fff;
  36. }
  37. </style>

Carousel走马灯 - 图5

渐显

切换效果为渐显。

  1. <template>
  2. <a-carousel effect="fade">
  3. <div><h3>1</h3></div>
  4. <div><h3>2</h3></div>
  5. <div><h3>3</h3></div>
  6. <div><h3>4</h3></div>
  7. </a-carousel>
  8. </template>
  9. <style scoped>
  10. /* For demo */
  11. .ant-carousel :deep(.slick-slide) {
  12. text-align: center;
  13. height: 160px;
  14. line-height: 160px;
  15. background: #364d79;
  16. overflow: hidden;
  17. }
  18. .ant-carousel :deep(.slick-slide h3) {
  19. color: #fff;
  20. }
  21. </style>

Carousel走马灯 - 图6

自动切换

定时切换下一张。

  1. <template>
  2. <a-carousel autoplay>
  3. <div><h3>1</h3></div>
  4. <div><h3>2</h3></div>
  5. <div><h3>3</h3></div>
  6. <div><h3>4</h3></div>
  7. </a-carousel>
  8. </template>
  9. <style scoped>
  10. /* For demo */
  11. .ant-carousel :deep(.slick-slide) {
  12. text-align: center;
  13. height: 160px;
  14. line-height: 160px;
  15. background: #364d79;
  16. overflow: hidden;
  17. }
  18. .ant-carousel :deep(.slick-slide h3) {
  19. color: #fff;
  20. }
  21. </style>

Carousel走马灯 - 图7

自定义分页

自定义分页展示。

  1. <template>
  2. <a-carousel arrows dots-class="slick-dots slick-thumb">
  3. <template #customPaging="props">
  4. <a>
  5. <img :src="getImgUrl(props.i)" />
  6. </a>
  7. </template>
  8. <div v-for="item in 4" :key="item">
  9. <img :src="baseUrl + 'abstract0' + item + '.jpg'" />
  10. </div>
  11. </a-carousel>
  12. </template>
  13. <script lang="ts">
  14. import { defineComponent } from 'vue';
  15. const baseUrl =
  16. 'https://raw.githubusercontent.com/vueComponent/ant-design-vue/master/components/vc-slick/assets/img/react-slick/';
  17. export default defineComponent({
  18. setup() {
  19. const getImgUrl = (i: number) => {
  20. return `${baseUrl}abstract0${i + 1}.jpg`;
  21. };
  22. return {
  23. baseUrl,
  24. getImgUrl,
  25. };
  26. },
  27. });
  28. </script>
  29. <style scoped>
  30. /* For demo */
  31. .ant-carousel :deep(.slick-dots) {
  32. position: relative;
  33. height: auto;
  34. }
  35. .ant-carousel :deep(.slick-slide img) {
  36. border: 5px solid #fff;
  37. display: block;
  38. margin: auto;
  39. max-width: 80%;
  40. }
  41. .ant-carousel :deep(.slick-arrow) {
  42. display: none !important;
  43. }
  44. .ant-carousel :deep(.slick-thumb) {
  45. bottom: 0px;
  46. }
  47. .ant-carousel :deep(.slick-thumb li) {
  48. width: 60px;
  49. height: 45px;
  50. }
  51. .ant-carousel :deep(.slick-thumb li img) {
  52. width: 100%;
  53. height: 100%;
  54. filter: grayscale(100%);
  55. }
  56. .ant-carousel :deep .slick-thumb li.slick-active img {
  57. filter: grayscale(0%);
  58. }
  59. </style>

Carousel走马灯 - 图8

自定义箭头

自定义箭头展示。

  1. <template>
  2. <a-carousel arrows>
  3. <template #prevArrow>
  4. <div class="custom-slick-arrow" style="left: 10px; zindex: 1">
  5. <left-circle-outlined />
  6. </div>
  7. </template>
  8. <template #nextArrow>
  9. <div class="custom-slick-arrow" style="right: 10px">
  10. <right-circle-outlined />
  11. </div>
  12. </template>
  13. <div><h3>1</h3></div>
  14. <div><h3>2</h3></div>
  15. <div><h3>3</h3></div>
  16. <div><h3>4</h3></div>
  17. </a-carousel>
  18. </template>
  19. <script lang="ts">
  20. import { LeftCircleOutlined, RightCircleOutlined } from '@ant-design/icons-vue';
  21. import { defineComponent } from 'vue';
  22. export default defineComponent({
  23. components: {
  24. LeftCircleOutlined,
  25. RightCircleOutlined,
  26. },
  27. });
  28. </script>
  29. <style scoped>
  30. /* For demo */
  31. .ant-carousel :deep(.slick-slide) {
  32. text-align: center;
  33. height: 160px;
  34. line-height: 160px;
  35. background: #364d79;
  36. overflow: hidden;
  37. }
  38. .ant-carousel :deep(.slick-arrow.custom-slick-arrow) {
  39. width: 25px;
  40. height: 25px;
  41. font-size: 25px;
  42. color: #fff;
  43. background-color: rgba(31, 45, 61, 0.11);
  44. opacity: 0.3;
  45. }
  46. .ant-carousel :deep(.custom-slick-arrow:before) {
  47. display: none;
  48. }
  49. .ant-carousel :deep(.custom-slick-arrow:hover) {
  50. opacity: 0.5;
  51. }
  52. .ant-carousel :deep(.slick-slide h3) {
  53. color: #fff;
  54. }
  55. </style>

API

参数说明类型默认值版本
afterChange切换面板的回调function(current)
autoplay是否自动切换booleanfalse
beforeChange切换面板的回调function(from, to)
dotPosition面板指示点位置,可选 top bottom left rightstringbottom1.5.0
dots是否显示面板指示点booleantrue
dotsClass面板指示点类名stringslick-dots
easing动画效果stringlinear
effect动画效果函数,可取 scrollx, fadestringscrollx

方法

名称描述版本
goTo(slideNumber, dontAnimate)切换到指定面板, dontAnimate = true 时,不使用动画
next()切换到下一面板
prev()切换到上一面板

更多参数可参考:vc-slick props