Carousel 走马灯
走马灯,轮播图
代码演示
基本
When using Carousel
in web projects, you may have problem about how to deal with variable item height.
issues/1002、nuka-carousel/issues/103
import { Carousel, WhiteSpace, WingBlank } from 'antd-mobile';
class App extends React.Component {
state = {
data: ['', '', ''],
initialHeight: 200,
}
componentDidMount() {
// simulate img loading
setTimeout(() => {
this.setState({
data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
});
}, 100);
}
render() {
const hProp = this.state.initialHeight ? { height: this.state.initialHeight } : {};
return (
<WingBlank>
<div className="sub-title">normal</div>
<Carousel
className="my-carousel"
autoplay={false}
infinite
selectedIndex={1}
swipeSpeed={35}
beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
afterChange={index => console.log('slide to', index)}
>
{this.state.data.map(ii => (
<a href="http://www.baidu.com" key={ii} style={hProp}>
<img
src={`https://zos.alipayobjects.com/rmsportal/${ii || 'QcWDkUhvYIVEcvtosxMF'}.png`}
alt="icon"
onLoad={() => {
// fire window resize event to change height
window.dispatchEvent(new Event('resize'));
this.setState({
initialHeight: null,
});
}}
/>
</a>
))}
</Carousel>
<WhiteSpace />
<div className="sub-title">vertical</div>
<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>
<WhiteSpace />
<div className="sub-title">lottery</div>
<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>
);
}
}
ReactDOM.render(<App />, mountNode);
.my-carousel {
background: #fff;
}
.my-carousel a {
display: inline-block;
width: 100%;
margin: 0; padding: 0;
}
.my-carousel a img {
width: 100%;
vertical-align: top;
}
.my-carousel .v-item {
height: 0.72rem;
line-height: 0.72rem;
padding-left: 0.2rem;
}
.sub-title {
color: #888;
font-size: .28rem;
padding: 30px 0 18px 0;
}
API
适用平台:WEB、React-Native
属性 | 说明 | 类型 | 默认值 |
---|
selectedIndex | 手动设置当前显示的索引 | number | 0 |
dots | 是否显示面板指示点 | Boolean | true |
vertical | 垂直显示(web 为内容,rn 为 pagination) | Boolean | false |
autoplay | 是否自动切换 | Boolean | false |
infinite | 是否循环播放 | Boolean | false |
afterChange | 切换面板后的回调函数 | (current: number): void | 无 |
dotStyle | 指示点样式 | Object | 无 |
dotActiveStyle | 当前激活的指示点样式 | Object | 无 |
swipeSpeed (web only ) | 滑动灵敏度 | number | 5 |
easing (web only ) | 动画效果 | String | linear |
beforeChange (web only ) | 切换面板前的回调函数 | (from: number, to: number): void | 无 |
onScrollBeginDrag (rn only ) | 见 react-native scrollView onScrollBeginDrag | (): void | 无 |
bounces (rn only ) | 见 react-native scrollView bounces | Boolean | true |
pagination (rn only ) | 自定义 pagination | (props) => React.ReactNode | |
更多参数可参考: https://github.com/react-component/nuka-carousel (web only
)