Carousel 走马灯

一组轮播的区域。

规则

  • 所展示信息属于同一个层级;同时,鼓励用户进行内容的浏览和探索。

  • 一般情况下,只进行横向滚动。

  • 默认自动轮播,当用户轻触时该区域时停止;允许用户向前、向后滚动 Carousel。

代码演示

走马灯

最简单的用法。

  1. import { Carousel, Flex } from 'antd-mobile';
  2. import classNames from 'classnames';
  3. const data = [
  4. {
  5. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  6. text: '文字',
  7. link: 'hehe',
  8. }, {
  9. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  10. text: '文字',
  11. link: 'hehe',
  12. }, {
  13. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  14. text: '文字',
  15. link: 'hehe',
  16. }, {
  17. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  18. text: '文字',
  19. link: 'hehe',
  20. }, {
  21. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  22. text: '文字',
  23. link: 'hehe',
  24. }, {
  25. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  26. text: '文字',
  27. link: 'hehe',
  28. }, {
  29. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  30. text: '文字',
  31. link: 'hehe',
  32. }, {
  33. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  34. text: '文字',
  35. link: 'hehe',
  36. }, {
  37. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  38. text: '文字',
  39. link: 'hehe',
  40. }, {
  41. icon: 'https://os.alipayobjects.com/rmsportal/IptWdCkrtkAUfjE.png',
  42. text: '文字',
  43. link: 'hehe',
  44. },
  45. ];
  46. const CarouselExample = React.createClass({
  47. componentWillMount() {
  48. this.clientWidth = document.documentElement.clientWidth;
  49. },
  50. render() {
  51. const dataLength = data.length;
  52. const FlexCount = Math.ceil(dataLength / 4);
  53. const gridContent = [];
  54. const carouselContent = [];
  55. const prefixCls = 'am-grid';
  56. const itemCls = classNames({
  57. [`${prefixCls}-item`]: true,
  58. });
  59. const flexItemStyle = {
  60. height: `${this.clientWidth / 4}px`,
  61. paddingTop: `${this.clientWidth / 16}px`,
  62. };
  63. for (let i = 0; i < FlexCount; i++) {
  64. const flexContent = [];
  65. for (let j = 0; j < 4; j++) {
  66. if ((i * 4) + j < dataLength) {
  67. flexContent.push(<Flex.Item
  68. className={itemCls}
  69. style={flexItemStyle}
  70. onClick={() => { this.props.onClick(data[(i * 4) + j], ((i * 4) + j)); }}
  71. key={`griditem-${(i * 4) + j}`}
  72. >
  73. <div className={`${prefixCls}-icon`} style={{ backgroundImage: `url(${data[(i * 4) + j].icon})` }} />
  74. <div className={`${prefixCls}-text`}>{data[(i * 4) + j].text}</div>
  75. </Flex.Item>);
  76. } else {
  77. flexContent.push(<Flex.Item style={flexItemStyle} key={`griditem-${(i * 4) + j}`} />);
  78. }
  79. }
  80. gridContent.push(<Flex key={`fridflex${i}`}>{flexContent}</Flex>);
  81. }
  82. const gridContentLength = gridContent.length;
  83. for (let k = 0, len = Math.ceil(gridContentLength / 2); k < len; k++) {
  84. if (k * 2 < gridContentLength) {
  85. carouselContent.push();
  86. }
  87. if ((k * 2) + 1 < gridContentLength) {
  88. carouselContent.push(<div
  89. key={`carouselitem-${(k * 2) + 1}`}
  90. >
  91. {gridContent[k * 2]}
  92. {gridContent[(k * 2) + 1]}
  93. </div>);
  94. } else {
  95. carouselContent.push(<div
  96. key={`carouselitem-${k * 2}`}
  97. >
  98. {gridContent[k * 2]}
  99. <Flex>
  100. <Flex.Item className={itemCls} style={flexItemStyle} />
  101. <Flex.Item className={itemCls} style={flexItemStyle} />
  102. <Flex.Item className={itemCls} style={flexItemStyle} />
  103. <Flex.Item className={itemCls} style={flexItemStyle} />
  104. </Flex>
  105. </div>);
  106. }
  107. }
  108. return (
  109. <Carousel>
  110. {carouselContent}
  111. </Carousel>
  112. );
  113. },
  114. });
  115. ReactDOM.render(<CarouselExample onClick={() => {}} />, mountNode);

图片切换

图片切换

  1. import { WhiteSpace, WingBlank, Flex, Carousel } from 'antd-mobile';
  2. const App = React.createClass({
  3. getInitialState() {
  4. return {
  5. current: 2,
  6. };
  7. },
  8. beforeSlide(from, to) {
  9. console.log(`slide from ${from} to ${to}`);
  10. },
  11. slideTo(index) {
  12. console.log('slide to', index);
  13. },
  14. render() {
  15. const settings = {
  16. dots: true,
  17. autoplay: true,
  18. infinite: true,
  19. selectedIndex: this.state.current,
  20. beforeChange: this.beforeSlide,
  21. afterChange: this.slideTo,
  22. };
  23. return (
  24. <div>
  25. <div className="pagination-container" >
  26. <WhiteSpace size="lg" />
  27. <WingBlank>
  28. <Carousel {...settings}>
  29. <Flex
  30. justify="center"
  31. className="flex-container-justify"
  32. >
  33. <h3>Carousel 1</h3>
  34. </Flex>
  35. <Flex
  36. justify="center"
  37. className="flex-container-justify"
  38. >
  39. <h3>Carousel 2</h3>
  40. </Flex>
  41. <Flex
  42. justify="center"
  43. className="flex-container-justify"
  44. >
  45. <h3>Carousel 3</h3>
  46. </Flex>
  47. </Carousel>
  48. </WingBlank>
  49. <WhiteSpace size="lg" />
  50. </div>
  51. </div>
  52. );
  53. },
  54. });
  55. ReactDOM.render(<App />, mountNode);
  1. .loading-example .title {
  2. margin-right: 0.2rem;
  3. }
  4. .slider {
  5. background: #fff;
  6. }
  7. .flex-container-justify {
  8. height: 3.6rem !important;
  9. }
  10. .flex-container-justify h3 {
  11. font-size: 0.36rem;
  12. font-weight: 300;
  13. text-align: center;
  14. }

Carousel走马灯 - 图1

API

参数说明类型默认值
selectedIndex手动设置当前显示的索引number0
dots是否显示面板指示点Booleantrue
vertical (web only)垂直显示Booleanfalse
autoplay是否自动切换Booleanfalse
infinite是否循环播放Booleanfalse
easing (web only)动画效果Stringlinear
beforeChange (web only)切换面板的回调function(from, to)
afterChange切换面板的回调function(current)
更多参数可参考:https://github.com/FormidableLabs/nuka-carousel