Carousel走马灯 - 图1

Carousel 走马灯

基本用法

  1. import { Carousel } from 'zarm';
  2. const ITEMS = [
  3. 'https://static.zhongan.com/website/health/zarm/images/banners/1.png',
  4. 'https://static.zhongan.com/website/health/zarm/images/banners/2.png',
  5. 'https://static.zhongan.com/website/health/zarm/images/banners/3.png',
  6. ];
  7. const contentRender = () => {
  8. return ITEMS.map((item, i) => {
  9. return (
  10. <div className="carousel__item__pic" key={+i}>
  11. <img src={item} alt="" draggable={false} />
  12. </div>
  13. );
  14. });
  15. }
  16. ReactDOM.render(
  17. <Carousel
  18. onChange={(index) => {
  19. console.log(`onChange: ${index}`);
  20. }}
  21. >
  22. {contentRender()}
  23. </Carousel>
  24. , mountNode);

纵向

  1. import { Carousel } from 'zarm';
  2. const ITEMS = [
  3. 'https://static.zhongan.com/website/health/zarm/images/banners/1.png',
  4. 'https://static.zhongan.com/website/health/zarm/images/banners/2.png',
  5. 'https://static.zhongan.com/website/health/zarm/images/banners/3.png',
  6. ];
  7. const contentRender = () => {
  8. return ITEMS.map((item, i) => {
  9. return (
  10. <div className="carousel__item__pic" key={+i}>
  11. <img src={item} alt="" draggable={false} />
  12. </div>
  13. );
  14. });
  15. }
  16. ReactDOM.render(
  17. <Carousel direction="up" height="48vw">{contentRender()}</Carousel>
  18. , mountNode);

循环轮播

  1. import { Carousel, Button } from 'zarm';
  2. const ITEMS = [
  3. 'https://static.zhongan.com/website/health/zarm/images/banners/1.png',
  4. 'https://static.zhongan.com/website/health/zarm/images/banners/2.png',
  5. 'https://static.zhongan.com/website/health/zarm/images/banners/3.png',
  6. ];
  7. const contentRender = () => {
  8. return ITEMS.map((item, i) => {
  9. return (
  10. <div className="carousel__item__pic" key={+i}>
  11. <img src={item} alt="" draggable={false} />
  12. </div>
  13. );
  14. });
  15. }
  16. class Demo extends React.Component {
  17. render() {
  18. return (
  19. <div>
  20. <Carousel
  21. ref={(ele) => { this.carousel = ele; }}
  22. loop
  23. direction="left"
  24. onChange={(index) => {
  25. console.log(`onChange: ${index}`);
  26. }}
  27. >
  28. {contentRender()}
  29. </Carousel>
  30. <div className="controls">
  31. <Button
  32. block
  33. size="sm"
  34. onClick={() => {
  35. this.carousel.onJumpTo(0);
  36. }}
  37. >
  38. 无动画切换指定页
  39. </Button>
  40. <Button
  41. block
  42. size="sm"
  43. onClick={() => {
  44. this.carousel.onSlideTo(2);
  45. }}
  46. >
  47. 滑动到指定页
  48. </Button>
  49. </div>
  50. </div>
  51. )
  52. }
  53. }
  54. ReactDOM.render(<Demo />, mountNode);

自动循环轮播

  1. import { Carousel } from 'zarm';
  2. const ITEMS = [
  3. 'https://static.zhongan.com/website/health/zarm/images/banners/1.png',
  4. 'https://static.zhongan.com/website/health/zarm/images/banners/2.png',
  5. 'https://static.zhongan.com/website/health/zarm/images/banners/3.png',
  6. ];
  7. const contentRender = () => {
  8. return ITEMS.map((item, i) => {
  9. return (
  10. <div className="carousel__item__pic" key={+i}>
  11. <img src={item} alt="" draggable={false} />
  12. </div>
  13. );
  14. });
  15. }
  16. ReactDOM.render(
  17. <Carousel
  18. autoPlay
  19. loop
  20. direction="left"
  21. onChangeEnd={(index) => {
  22. console.log(`onChangeEnd: ${index}`);
  23. }}
  24. >
  25. {contentRender()}
  26. </Carousel>
  27. , mountNode);

API

属性类型默认值说明
directionstring'left'滑动方向,可选值 leftrightupdown
heightnumber | string160设置轮播区域高度
activeIndexnumber0当前页面的索引
loopbooleanfalse是否循环
swipeablebooleantrue是否支持拖拽滑动
autoPlaybooleanfalse是否自动轮播
autoPlayIntervalTimenumber3000自动轮播时间间隔,单位:毫秒
moveDistanceRationumber0.5移动距离比例临界点
moveTimeSpannumber300移动时间跨度临界点,单位:毫秒
animationDurationnumber300动画执行时间,单位:毫秒
showPaginationbooleantrue是否显示分页器
onChange(activeIndex?: number) => void-值变化时触发的回调函数
onChangeEnd(activeIndex?: number) => void-值变化动画结束后触发的回调函数