Carousel 走马灯

走马灯,轮播图

代码演示

基本

When using Carousel in web projects, you may have problem about how to deal with variable item height.

issues/1002nuka-carousel/issues/103

  1. import { Carousel, WhiteSpace, WingBlank } from 'antd-mobile';
  2. class App extends React.Component {
  3. state = {
  4. data: ['', '', ''],
  5. initialHeight: 200,
  6. }
  7. componentDidMount() {
  8. // simulate img loading
  9. setTimeout(() => {
  10. this.setState({
  11. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  12. });
  13. }, 100);
  14. }
  15. render() {
  16. const hProp = this.state.initialHeight ? { height: this.state.initialHeight } : {};
  17. return (
  18. <WingBlank>
  19. <div className="sub-title">normal</div>
  20. <Carousel
  21. className="my-carousel"
  22. autoplay={false}
  23. infinite
  24. selectedIndex={1}
  25. swipeSpeed={35}
  26. beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
  27. afterChange={index => console.log('slide to', index)}
  28. >
  29. {this.state.data.map(ii => (
  30. <a href="http://www.baidu.com" key={ii} style={hProp}>
  31. <img
  32. src={`https://zos.alipayobjects.com/rmsportal/${ii || 'QcWDkUhvYIVEcvtosxMF'}.png`}
  33. alt="icon"
  34. onLoad={() => {
  35. // fire window resize event to change height
  36. window.dispatchEvent(new Event('resize'));
  37. this.setState({
  38. initialHeight: null,
  39. });
  40. }}
  41. />
  42. </a>
  43. ))}
  44. </Carousel>
  45. <WhiteSpace />
  46. <div className="sub-title">vertical</div>
  47. <Carousel className="my-carousel"
  48. vertical
  49. dots={false}
  50. dragging={false}
  51. swiping={false}
  52. autoplay
  53. infinite
  54. >
  55. <div className="v-item">Carousel 1</div>
  56. <div className="v-item">Carousel 2</div>
  57. <div className="v-item">Carousel 3</div>
  58. </Carousel>
  59. <WhiteSpace />
  60. <div className="sub-title">lottery</div>
  61. <Carousel className="my-carousel"
  62. vertical
  63. dots={false}
  64. dragging={false}
  65. swiping={false}
  66. autoplay
  67. infinite
  68. speed={200}
  69. autoplayInterval={300}
  70. resetAutoplay={false}
  71. >
  72. {['ring', 'ruby', 'iPhone', 'iPod', 'sorry', 'tourism', 'coke', 'ticket', 'note'].map(type => (
  73. <div className="v-item" key={type}>{type}</div>
  74. ))}
  75. </Carousel>
  76. </WingBlank>
  77. );
  78. }
  79. }
  80. ReactDOM.render(<App />, mountNode);
  1. .my-carousel {
  2. background: #fff;
  3. }
  4. .my-carousel a {
  5. display: inline-block;
  6. width: 100%;
  7. margin: 0; padding: 0;
  8. }
  9. .my-carousel a img {
  10. width: 100%;
  11. vertical-align: top;
  12. }
  13. .my-carousel .v-item {
  14. height: 0.72rem;
  15. line-height: 0.72rem;
  16. padding-left: 0.2rem;
  17. }
  18. .sub-title {
  19. color: #888;
  20. font-size: .28rem;
  21. padding: 30px 0 18px 0;
  22. }

Carousel走马灯 - 图1

API

适用平台:WEB、React-Native
属性说明类型默认值
selectedIndex手动设置当前显示的索引number0
dots是否显示面板指示点Booleantrue
vertical垂直显示(web 为内容,rn 为 pagination)Booleanfalse
autoplay是否自动切换Booleanfalse
infinite是否循环播放Booleanfalse
afterChange切换面板后的回调函数(current: number): void
dotStyle指示点样式Object
dotActiveStyle当前激活的指示点样式Object
swipeSpeed (web only)滑动灵敏度number5
easing (web only)动画效果Stringlinear
beforeChange (web only)切换面板前的回调函数(from: number, to: number): void
onScrollBeginDrag (rn only)见 react-native scrollView onScrollBeginDrag(): void
bounces (rn only)见 react-native scrollView bouncesBooleantrue
pagination (rn only)自定义 pagination(props) => React.ReactNode
更多参数可参考: https://github.com/react-component/nuka-carousel (web only)