Carousel 走马灯
走马灯,轮播图
代码演示
基本
When using Carousel
, you may have problem about how to deal with variable item height.
issues/1002、nuka-carousel/issues/103
import { Carousel, WingBlank } from 'antd-mobile';
class App extends React.Component {
state = {
data: ['1', '2', '3'],
imgHeight: 176,
}
componentDidMount() {
// simulate img loading
setTimeout(() => {
this.setState({
data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
});
}, 100);
}
render() {
return (
<WingBlank>
<Carousel
autoplay={false}
infinite
beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
afterChange={index => console.log('slide to', index)}
>
{this.state.data.map(val => (
<a
key={val}
href="http://www.alipay.com"
style={{ display: 'inline-block', width: '100%', height: this.state.imgHeight }}
>
<img
src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
alt=""
style={{ width: '100%', verticalAlign: 'top' }}
onLoad={() => {
// fire window resize event to change height
window.dispatchEvent(new Event('resize'));
this.setState({ imgHeight: 'auto' });
}}
/>
</a>
))}
</Carousel>
</WingBlank>
);
}
}
ReactDOM.render(<App />, mountNode);
子元素数量变化
import { Carousel, Button, WhiteSpace, WingBlank } from 'antd-mobile';
class App extends React.Component {
state = {
data: ['1', '2', '3'],
imgHeight: 176,
slideIndex: 2,
}
componentDidMount() {
// simulate img loading
setTimeout(() => {
this.setState({
data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
});
}, 100);
}
componentDidUpdate() {
// After the new child element is rendered, change the slideIndex
// https://github.com/FormidableLabs/nuka-carousel/issues/327
if (this.state.slideIndex !== this.state.data.length - 1) {
/* eslint react/no-did-update-set-state: 0 */
this.setState({ slideIndex: this.state.data.length - 1 });
}
}
render() {
return (
<WingBlank>
<Button
onClick={() => {
this.setState({
data: this.state.data.concat('AiyWuByWklrrUDlFignR'),
});
}}
>Click me to add child</Button>
<WhiteSpace />
<Carousel
autoplay={false}
infinite
selectedIndex={this.state.slideIndex}
beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
afterChange={index => console.log('slide to', index)}
>
{this.state.data.map((val, index) => (
<a
key={val + index}
href="http://www.alipay.com"
style={{ display: 'inline-block', width: '100%', height: this.state.imgHeight }}
>
<img
src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
alt=""
style={{ width: '100%', verticalAlign: 'top' }}
onLoad={() => {
// fire window resize event to change height
window.dispatchEvent(new Event('resize'));
this.setState({ imgHeight: 'auto' });
}}
/>
</a>
))}
</Carousel>
</WingBlank>
);
}
}
ReactDOM.render(<App />, mountNode);
带间距
import { Carousel, WingBlank } from 'antd-mobile';
class App extends React.Component {
state = {
data: ['1', '2', '3'],
imgHeight: 176,
}
componentDidMount() {
// simulate img loading
setTimeout(() => {
this.setState({
data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
});
}, 100);
}
render() {
return (
<WingBlank>
<Carousel className="space-carousel"
frameOverflow="visible"
cellSpacing={10}
slideWidth={0.8}
autoplay
infinite
beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
afterChange={index => this.setState({ slideIndex: index })}
>
{this.state.data.map((val, index) => (
<a
key={val}
href="http://www.alipay.com"
style={{
display: 'block',
position: 'relative',
top: this.state.slideIndex === index ? -10 : 0,
height: this.state.imgHeight,
boxShadow: '2px 1px 1px rgba(0, 0, 0, 0.2)',
}}
>
<img
src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
alt=""
style={{ width: '100%', verticalAlign: 'top' }}
onLoad={() => {
// fire window resize event to change height
window.dispatchEvent(new Event('resize'));
this.setState({ imgHeight: 'auto' });
}}
/>
</a>
))}
</Carousel>
</WingBlank>
);
}
}
ReactDOM.render(<App />, mountNode);
.space-carousel {
padding: 16px;
background: #DEF1E5;
overflow: hidden;
}
竖向
import { Carousel, WingBlank } from 'antd-mobile';
ReactDOM.render(
<WingBlank>
<Carousel className="my-carousel"
vertical
dots={false}
dragging={false}
swiping={false}
autoplay
infinite
>
<div className="v-item">carousel 1</div>
<div className="v-item">carousel 2</div>
<div className="v-item">carousel 3</div>
</Carousel>
</WingBlank>
, mountNode);
.my-carousel .v-item {
height: 36px;
line-height: 36px;
padding-left: 10px;
}
抽奖
import { Carousel, WingBlank } from 'antd-mobile';
ReactDOM.render(
<WingBlank>
<Carousel className="my-carousel"
vertical
dots={false}
dragging={false}
swiping={false}
autoplay
infinite
speed={200}
autoplayInterval={300}
resetAutoplay={false}
>
{['ring', 'ruby', 'iPhone', 'iPod', 'sorry', 'tourism', 'coke', 'ticket', 'note'].map(type => (
<div className="v-item" key={type}>{type}</div>
))}
</Carousel>
</WingBlank>
, mountNode);
.my-carousel .v-item {
height: 36px;
line-height: 36px;
padding-left: 10px;
}
API
属性 | 说明 | 类型 | 默认值 |
---|
selectedIndex | 手动设置当前显示的索引 | number | 0 |
dots | 是否显示面板指示点 | Boolean | true |
vertical | 垂直显示 | Boolean | false |
autoplay | 是否自动切换 | Boolean | false |
autoplayInterval | 自动切换的时间间隔 | Number | 3000 |
infinite | 是否循环播放 | Boolean | false |
afterChange | 切换面板后的回调函数 | (current: number): void | 无 |
dotStyle | 指示点样式 | Object | 无 |
dotActiveStyle | 当前激活的指示点样式 | Object | 无 |
frameOverflow | 设置 slider frame 的 overflow 样式 | string | hidden |
cellSpacing | 项目之间的间距,以px 为单位 | number | - |
slideWidth | 手动设置项目宽度. 可以是slideWidth="20px" ,也可以是相对容器的百分比slideWidth={0.8} | string / number | - |
easing | 缓动函数,你可以使用这里提供的其他函数 | Function | easeOutCirc |
swipeSpeed | 滑动灵敏度 | number | 12 |
beforeChange | 切换面板前的回调函数 | (from: number, to: number): void | 无 |