Carousel
A carousel component. Scales with its container.
When To Use#
When there is a group of content on the same level.
When there is insufficient content space, it can be used to save space in the form of a revolving door.
Commonly used for a group of pictures/cards.
Examples
TypeScript
JavaScript
import { Carousel } from 'antd';
import React from 'react';
const contentStyle: React.CSSProperties = {
margin: 0,
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
const App: React.FC = () => {
const onChange = (currentSlide: number) => {
console.log(currentSlide);
};
return (
<Carousel afterChange={onChange}>
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>
);
};
export default App;
TypeScript
JavaScript
import { Carousel } from 'antd';
import React from 'react';
const contentStyle: React.CSSProperties = {
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
const App: React.FC = () => (
<Carousel autoplay>
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>
);
export default App;
TypeScript
JavaScript
import type { RadioChangeEvent } from 'antd';
import { Carousel, Radio } from 'antd';
import type { DotPosition } from 'antd/es/carousel';
import React, { useState } from 'react';
const contentStyle: React.CSSProperties = {
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
const App: React.FC = () => {
const [dotPosition, setDotPosition] = useState<DotPosition>('top');
const handlePositionChange = ({ target: { value } }: RadioChangeEvent) => {
setDotPosition(value);
};
return (
<>
<Radio.Group onChange={handlePositionChange} value={dotPosition} style={{ marginBottom: 8 }}>
<Radio.Button value="top">Top</Radio.Button>
<Radio.Button value="bottom">Bottom</Radio.Button>
<Radio.Button value="left">Left</Radio.Button>
<Radio.Button value="right">Right</Radio.Button>
</Radio.Group>
<Carousel dotPosition={dotPosition}>
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>
</>
);
};
export default App;
TypeScript
JavaScript
import { Carousel } from 'antd';
import React from 'react';
const contentStyle: React.CSSProperties = {
height: '160px',
color: '#fff',
lineHeight: '160px',
textAlign: 'center',
background: '#364d79',
};
const App: React.FC = () => (
<Carousel effect="fade">
<div>
<h3 style={contentStyle}>1</h3>
</div>
<div>
<h3 style={contentStyle}>2</h3>
</div>
<div>
<h3 style={contentStyle}>3</h3>
</div>
<div>
<h3 style={contentStyle}>4</h3>
</div>
</Carousel>
);
export default App;
API#
Property | Description | Type | Default | Version |
---|---|---|---|---|
autoplay | Whether to scroll automatically | boolean | false | |
dotPosition | The position of the dots, which can be one of top bottom left right | string | bottom | |
dots | Whether to show the dots at the bottom of the gallery, object for dotsClass and any others | boolean | { className?: string } | true | |
easing | Transition interpolation function name | string | linear | |
effect | Transition effect | scrollx | fade | scrollx | |
afterChange | Callback function called after the current index changes | function(current) | - | |
beforeChange | Callback function called before the current index changes | function(from, to) | - |
Methods#
Name | Description |
---|---|
goTo(slideNumber, dontAnimate) | Go to slide index, if dontAnimate=true, it happens without animation |
next() | Change current slide to next slide |
prev() | Change current slide to previous slide |
Find more APIs in react-slick documentation.
FAQ#
How to add custom arrows?#
See #12479.