For AI agents: the complete documentation index is available at https://ifhover.github.io/leaf-ui/llms.txt, the full documentation bundle is available at https://ifhover.github.io/leaf-ui/llms-full.txt, and this page is available as Markdown at https://ifhover.github.io/leaf-ui/components/carousel.md.

Carousel 轮播

依次浏览图片或内容。

基础用法

basic.tsx
import { Carousel } from '@ifhover/leaf-ui';

const slides = [
  { key: 'plan', zh: '计划', en: 'Plan', color: '#edf6ee' },
  { key: 'create', zh: '创作', en: 'Create', color: '#eef3ff' },
  { key: 'share', zh: '分享', en: 'Share', color: '#fff5df' },
];

export function CarouselBasic() {
  return (
    <Carousel aria-label={'项目阶段'}>
      {slides.map((slide) => (
        <div
          key={slide.key}
          style={{
            display: 'grid',
            placeItems: 'center',
            height: 180,
            background: slide.color,
            color: '#24372c',
            fontSize: 24,
          }}
        >
          {slide.zh}
        </div>
      ))}
    </Carousel>
  );
}

自动播放

basic.tsx
import { Carousel } from '@ifhover/leaf-ui';

const slides = [
  { key: 'plan', zh: '计划', en: 'Plan', color: '#edf6ee' },
  { key: 'create', zh: '创作', en: 'Create', color: '#eef3ff' },
  { key: 'share', zh: '分享', en: 'Share', color: '#fff5df' },
];

export function CarouselAutoplay() {
  return (
    <Carousel autoplay={5000} arrows={false} aria-label={'精选内容'}>
      {slides.map((slide) => (
        <div
          key={slide.key}
          style={{
            display: 'grid',
            placeItems: 'center',
            height: 150,
            background: slide.color,
            color: '#24372c',
          }}
        >
          {slide.zh}
        </div>
      ))}
    </Carousel>
  );
}

CarouselHandle

属性类型默认值说明
goTo(index: number) => void—必需。切换到指定的索引。
next() => void—必需。切换到下一项。
previous() => void—必需。切换到上一项。
属性类型默认值说明
activeIndexnumber—受控的当前幻灯片索引。
defaultActiveIndexnumber0初始幻灯片索引。
onChange(index: number) => void—值发生变化时调用。
arrowsbooleantrue显示前后切换按钮。
dotsbooleantrue显示幻灯片指示器。
autoplayboolean | numberfalse启用自动播放;数字表示间隔毫秒数。
infinitebooleantrue允许循环切换。
pauseOnHoverbooleantrue鼠标悬停时暂停播放;焦点进入也会暂停。

类型与交互

组装可复用配置时,可导入对应的属性和数据类型。

import type { CarouselHandle, CarouselProps } from '@ifhover/leaf-ui';

为仅展示图标的操作提供可访问名称。交互控件支持键盘操作,并跟随 ConfigProvider 的主题和减少动画设置。