Carousel 走马灯

走马灯,轮播图

代码演示

基本

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

issues/1002nuka-carousel/issues/103

  1. import { Carousel, WingBlank } from 'antd-mobile';
  2. class App extends React.Component {
  3. state = {
  4. data: ['1', '2', '3'],
  5. imgHeight: 176,
  6. }
  7. componentDidMount() {
  8. // simulate img loading
  9. setTimeout(() => {
  10. this.setState({
  11. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  12. });
  13. }, 100);
  14. }
  15. render() {
  16. return (
  17. <WingBlank>
  18. <Carousel
  19. autoplay={false}
  20. infinite
  21. beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
  22. afterChange={index => console.log('slide to', index)}
  23. >
  24. {this.state.data.map(val => (
  25. <a
  26. key={val}
  27. href="http://www.alipay.com"
  28. style={{ display: 'inline-block', width: '100%', height: this.state.imgHeight }}
  29. >
  30. <img
  31. src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
  32. alt=""
  33. style={{ width: '100%', verticalAlign: 'top' }}
  34. onLoad={() => {
  35. // fire window resize event to change height
  36. window.dispatchEvent(new Event('resize'));
  37. this.setState({ imgHeight: 'auto' });
  38. }}
  39. />
  40. </a>
  41. ))}
  42. </Carousel>
  43. </WingBlank>
  44. );
  45. }
  46. }
  47. ReactDOM.render(<App />, mountNode);

子元素数量变化

  1. import { Carousel, Button, WhiteSpace, WingBlank } from 'antd-mobile';
  2. class App extends React.Component {
  3. state = {
  4. data: ['1', '2', '3'],
  5. imgHeight: 176,
  6. slideIndex: 2,
  7. }
  8. componentDidMount() {
  9. // simulate img loading
  10. setTimeout(() => {
  11. this.setState({
  12. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  13. });
  14. }, 100);
  15. }
  16. componentDidUpdate() {
  17. // After the new child element is rendered, change the slideIndex
  18. // https://github.com/FormidableLabs/nuka-carousel/issues/327
  19. if (this.state.slideIndex !== this.state.data.length - 1) {
  20. /* eslint react/no-did-update-set-state: 0 */
  21. this.setState({ slideIndex: this.state.data.length - 1 });
  22. }
  23. }
  24. render() {
  25. return (
  26. <WingBlank>
  27. <Button
  28. onClick={() => {
  29. this.setState({
  30. data: this.state.data.concat('AiyWuByWklrrUDlFignR'),
  31. });
  32. }}
  33. >Click me to add child</Button>
  34. <WhiteSpace />
  35. <Carousel
  36. autoplay={false}
  37. infinite
  38. selectedIndex={this.state.slideIndex}
  39. beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
  40. afterChange={index => console.log('slide to', index)}
  41. >
  42. {this.state.data.map((val, index) => (
  43. <a
  44. key={val + index}
  45. href="http://www.alipay.com"
  46. style={{ display: 'inline-block', width: '100%', height: this.state.imgHeight }}
  47. >
  48. <img
  49. src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
  50. alt=""
  51. style={{ width: '100%', verticalAlign: 'top' }}
  52. onLoad={() => {
  53. // fire window resize event to change height
  54. window.dispatchEvent(new Event('resize'));
  55. this.setState({ imgHeight: 'auto' });
  56. }}
  57. />
  58. </a>
  59. ))}
  60. </Carousel>
  61. </WingBlank>
  62. );
  63. }
  64. }
  65. ReactDOM.render(<App />, mountNode);

带间距

  1. import { Carousel, WingBlank } from 'antd-mobile';
  2. class App extends React.Component {
  3. state = {
  4. data: ['1', '2', '3'],
  5. imgHeight: 176,
  6. }
  7. componentDidMount() {
  8. // simulate img loading
  9. setTimeout(() => {
  10. this.setState({
  11. data: ['AiyWuByWklrrUDlFignR', 'TekJlZRVCjLFexlOCuWn', 'IJOtIlfsYdTyaDTRVrLI'],
  12. });
  13. }, 100);
  14. }
  15. render() {
  16. return (
  17. <WingBlank>
  18. <Carousel className="space-carousel"
  19. frameOverflow="visible"
  20. cellSpacing={10}
  21. slideWidth={0.8}
  22. autoplay
  23. infinite
  24. beforeChange={(from, to) => console.log(`slide from ${from} to ${to}`)}
  25. afterChange={index => this.setState({ slideIndex: index })}
  26. >
  27. {this.state.data.map((val, index) => (
  28. <a
  29. key={val}
  30. href="http://www.alipay.com"
  31. style={{
  32. display: 'block',
  33. position: 'relative',
  34. top: this.state.slideIndex === index ? -10 : 0,
  35. height: this.state.imgHeight,
  36. boxShadow: '2px 1px 1px rgba(0, 0, 0, 0.2)',
  37. }}
  38. >
  39. <img
  40. src={`https://zos.alipayobjects.com/rmsportal/${val}.png`}
  41. alt=""
  42. style={{ width: '100%', verticalAlign: 'top' }}
  43. onLoad={() => {
  44. // fire window resize event to change height
  45. window.dispatchEvent(new Event('resize'));
  46. this.setState({ imgHeight: 'auto' });
  47. }}
  48. />
  49. </a>
  50. ))}
  51. </Carousel>
  52. </WingBlank>
  53. );
  54. }
  55. }
  56. ReactDOM.render(<App />, mountNode);
  1. .space-carousel {
  2. padding: 16px;
  3. background: #DEF1E5;
  4. overflow: hidden;
  5. }

竖向

  1. import { Carousel, WingBlank } from 'antd-mobile';
  2. ReactDOM.render(
  3. <WingBlank>
  4. <Carousel className="my-carousel"
  5. vertical
  6. dots={false}
  7. dragging={false}
  8. swiping={false}
  9. autoplay
  10. infinite
  11. >
  12. <div className="v-item">carousel 1</div>
  13. <div className="v-item">carousel 2</div>
  14. <div className="v-item">carousel 3</div>
  15. </Carousel>
  16. </WingBlank>
  17. , mountNode);
  1. .my-carousel .v-item {
  2. height: 36px;
  3. line-height: 36px;
  4. padding-left: 10px;
  5. }

抽奖

  1. import { Carousel, WingBlank } from 'antd-mobile';
  2. ReactDOM.render(
  3. <WingBlank>
  4. <Carousel className="my-carousel"
  5. vertical
  6. dots={false}
  7. dragging={false}
  8. swiping={false}
  9. autoplay
  10. infinite
  11. speed={200}
  12. autoplayInterval={300}
  13. resetAutoplay={false}
  14. >
  15. {['ring', 'ruby', 'iPhone', 'iPod', 'sorry', 'tourism', 'coke', 'ticket', 'note'].map(type => (
  16. <div className="v-item" key={type}>{type}</div>
  17. ))}
  18. </Carousel>
  19. </WingBlank>
  20. , mountNode);
  1. .my-carousel .v-item {
  2. height: 36px;
  3. line-height: 36px;
  4. padding-left: 10px;
  5. }

Carousel走马灯 - 图1

API

属性说明类型默认值
selectedIndex手动设置当前显示的索引number0
dots是否显示面板指示点Booleantrue
vertical垂直显示Booleanfalse
autoplay是否自动切换Booleanfalse
autoplayInterval自动切换的时间间隔Number3000
infinite是否循环播放Booleanfalse
afterChange切换面板后的回调函数(current: number): void
dotStyle指示点样式Object
dotActiveStyle当前激活的指示点样式Object
frameOverflow设置 slider frame 的 overflow 样式stringhidden
cellSpacing项目之间的间距,以px为单位number-
slideWidth手动设置项目宽度. 可以是slideWidth="20px",也可以是相对容器的百分比slideWidth={0.8}string / number-
easing缓动函数,你可以使用这里提供的其他函数FunctioneaseOutCirc
swipeSpeed滑动灵敏度number12
beforeChange切换面板前的回调函数(from: number, to: number): void