---
localeCode: zh-CN
order: 48
category: 展示类
title: Carousel 轮播图
icon: doc-carousel
brief: 轮播图是一种媒体组件,可以在可视化应用中展示多张图片轮流播放的效果。
---
## 代码演示
### 如何引入
```jsx import
import { Carousel } from '@douyinfe/semi-ui';
```
### 基本用法
基本用法
```jsx live=true dir="column"
import React from 'react';
import { Carousel, Typography, Space } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px',
color: '#1C1F23'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
);
};
```
### 主题切换
默认定义了三种主题: `primary`、`light`、`dark`
```jsx live=true dir="column"
import React from 'react';
import { Carousel, RadioGroup, Radio, Space, Typography } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const [theme, setTheme] = useState('primary');
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
主题
setTheme(e.target.value)} value={theme}>
primary
light
dark
);
};
```
### 指示器
指示器可以调节类型、位置、尺寸
类型: `dot`、`line`、`columnar`
位置: `left`、`center`、`right`
尺寸: `small`、`medium`
```jsx live=true dir="column"
import React from 'react';
import { Carousel, RadioGroup, Radio, Space, Typography } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const [size, setSize] = useState('small');
const [type, setType] = useState('dot');
const [position, setPosition] = useState('left');
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
类型
setType(e.target.value)} value={type}>
dot
line
columnar
位置
setPosition(e.target.value)} value={position}>
left
center
right
尺寸
setSize(e.target.value)} value={size}>
small
medium
);
};
```
### 箭头
通过 showArrow 属性控制箭头是否可见
如果箭头可见,通过 arrowType 属性控制箭头展示的时机
```jsx live=true dir="column"
import React from 'react';
import { Carousel, RadioGroup, Radio, Space, Typography } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const [arrowType, setArrowTypew] = useState('always');
const [show, setShow] = useState(true);
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
展示箭头
setShow(e.target.value)} value={show}>
show
hide
展示时机
setArrowTypew(e.target.value)} value={arrowType}>
always
hover
);
};
```
### 定制箭头
通过 arrowProps 属性定制箭头样式和点击事件
```jsx live=true dir="column"
import React from 'react';
import { Carousel, Typography, Space } from '@douyinfe/semi-ui';
import { IconArrowLeft, IconArrowRight } from "@douyinfe/semi-icons";
class CarouselDemo extends React.Component {
constructor(props) {
super(props);
this.imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
this.textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
this.arrowProps = {
leftArrow: { children: },
rightArrow: { children: },
};
};
renderLogo() {
return (
);
};
render() {
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
return (
{
this.imgList.map((src, index) => {
return (
{this.renderLogo()}
{this.textList[index][0]}
{this.textList[index][1]}
{this.textList[index][2]}
);
})
}
);
}
}
```
### 播放参数
通过给 autoPlay 传入参数 interval 控制两张图片之间的时间间隔,传入 hoverToPause 控制鼠标放置在图片上时是否停止播放
```jsx live=true dir="column"
import React from 'react';
import { Carousel, Typography, Space } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
);
};
```
### 动画效果与切换速度
通过给 animation 属性控制动画,可选值有 `fade`,`slide`
通过给 speed 属性控制两张图片之间的切换时间,单位为ms
```jsx live=true dir="column"
import React from 'react';
import { Carousel, Typography, Space } from '@douyinfe/semi-ui';
() => {
const { Title, Paragraph } = Typography;
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const renderLogo = () => {
return (
);
};
const imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
const textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
return (
{
imgList.map((src, index) => {
return (
{renderLogo()}
{textList[index][0]}
{textList[index][1]}
{textList[index][2]}
);
})
}
);
};
```
### 受控的轮播图
```jsx live=true dir="column"
import React from 'react';
import { Carousel, Space, Typography } from '@douyinfe/semi-ui';
class CarouselDemo extends React.Component {
constructor(props) {
super(props);
this.imgList = [
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-1.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-2.png',
'https://lf3-static.bytednsdoc.com/obj/eden-cn/hjeh7pldnulm/SemiDocs/bg-3.png',
];
this.textList = [
['Semi 设计管理系统', '从 Semi Design,到 Any Design', '快速定制你的设计系统,并应用在设计稿和代码中'],
['Semi 物料市场', '面向业务场景的定制化组件,支持线上预览和调试', '内容由 Semi Design 用户共建'],
['Semi Pro (开发中)', '基于 40+ 真实组件代码设计', '海量页面模板前端代码一键转'],
];
this.state = {
activeIndex: 0,
};
}
renderLogo() {
return (
);
};
onChange(activeIndex){
this.setState({ activeIndex });
}
render() {
const style = {
width: '100%',
height: '400px',
};
const titleStyle = {
position: 'absolute',
top: '100px',
left: '100px'
};
const colorStyle = {
color: '#1C1F23'
};
const { activeIndex } = this.state;
return (
{
this.imgList.map((src, index) => {
return (
{this.renderLogo()}
{this.textList[index][0]}
{this.textList[index][1]}
{this.textList[index][2]}
);
})
}
);
}
}
```
### API 参考
**Carousel**
|属性 |说明 |类型 |默认值 |版本 |
|------------------|---------------------------------------------------------------|------------------|------|------|
|activeIndex |受控属性 |number |- |2.10.0|
|animation |切换动画,可选值:`fade`,`slide` |"fade" \| "slide" |"slide"|2.10.0|
|arrowProps |箭头参数,用于自定义箭头样式和点击事件 |() => { leftArrow: ArrowButton, rightArrow: ArrowButton } |- |2.10.0|
|autoPlay |是否自动循环展示,或者传入 { interval: 自动切换时间间隔(默认: 2000), hoverToPause: 鼠标悬浮时是否暂停自动切换(默认: true) } |boolean | { interval?: number, hoverToPause?: boolean } |true |2.10.0|
|className |样式类名 |string |- |2.10.0|
|defaultActiveIndex|初始化时默认展示的索引 |number |0 |2.10.0|
|indicatorPosition |指示器位置,可选值有: `left`、`center`、`right` |"left" \| "center" \| "right" |"center"|2.10.0|
|indicatorSize |指示器尺寸,可选值有: `small`、`medium` |"small" \| "medium"|"small"|2.10.0|
|indicatorType |指示器类型,可选值有: `dot`、`line`、`columnar` |"dot" \| "line" \| "columnar"|"dot"|2.10.0|
|theme |指示器和箭头主题,可选值有: `primary`、`light`、`dark` |"primary" \| "light" \| "dark" |"light"|2.10.0|
|onChange |图片切换时的回调 |(index: number, preIndex: number) => void |- |2.10.0|
|arrowType |箭头展示时机,可选值有: `hover`、`always` |"hover" \| "always"|always |2.10.0|
|showArrow |是否展示箭头 |boolean |true |2.10.0|
|showIndicator |是否展示指示器 |boolean |true |2.10.0|
|slideDirection |动画效果为`slide`时的滑动的方向,可选值有: `left`、`right` |"left" \| "right" |"left" |2.10.0|
|speed |切换速度,单位ms |number |300 |2.10.0|
|style |内联样式 |CSSProperties |- |2.10.0|
|trigger |指示器触发的时机,可选值有: `hover`、`click` |"hover" \| "click"|- |2.10.0|
**ArrowButton**
|属性 |说明 |类型 |默认值 |版本 |
|------------------|---------------------------------------------------------------|------------------|------|------|
|props |箭头div上的可传参数,包括style, onClick事件等 | React.DetailedHTMLProps, HTMLDivElement\> |- |2.10.0|
|children |箭头自定义Icon |React.ReactNode |- |2.10.0|
**Method()**
| 方法 | 说明 | 版本 |
| ----------------- | -------------------------- | ------ |
| play() | 播放 | 2.10.0 |
| stop() | 停止播放 | 2.10.0 |
| goTo(targetIndex) | 切换到指定位置 | 2.10.0 |
| prev() | 切换到上一位置 | 2.10.0 |
| next() | 切换到下一位置 | 2.10.0 |
## 设计变量