|
| 1 | +Carousel 走马灯 |
| 2 | +=== |
| 3 | + |
| 4 | +[](https://uiwjs.github.io/npm-unpkg/#/pkg/@uiw/react-carousel/file/README.md) |
| 5 | +[](https://www.npmjs.com/package/@uiw/react-carousel) |
| 6 | +[](https://npmjs.com/@uiw/react-carousel) |
| 7 | + |
| 8 | +滚动播放。在 v4.15.0+ 添加。 |
| 9 | + |
| 10 | +## 基础用法 |
| 11 | + |
| 12 | +最简单的用法。 |
| 13 | + |
| 14 | +<!--rehype:bgWhite=true&codeSandbox=true&codePen=true--> |
| 15 | +```jsx |
| 16 | +import React from 'react'; |
| 17 | +import { Carousel } from 'uiw'; |
| 18 | + |
| 19 | +function Demo() { |
| 20 | + return ( |
| 21 | + <Carousel> |
| 22 | + <div style={{ height: '100%', background: '#1EABCD' }}> |
| 23 | + <span>1</span> |
| 24 | + </div> |
| 25 | + <div style={{ height: '100%', background: '#393b46' }}> |
| 26 | + <span>2</span> |
| 27 | + </div> |
| 28 | + <div style={{ height: '100%', background: '#008EF0' }}> |
| 29 | + <span>3</span> |
| 30 | + </div> |
| 31 | + <div style={{ height: '100%', background: '#393E48' }}> |
| 32 | + <span>4</span> |
| 33 | + </div> |
| 34 | + </Carousel> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +ReactDOM.render(<Demo />, _mount_); |
| 39 | +``` |
| 40 | + |
| 41 | +## 控制播放频率 |
| 42 | + |
| 43 | +palyTime设置每帧停留时间,scrollTime设置切换帧的速度 |
| 44 | + |
| 45 | +<!--rehype:bgWhite=true&codeSandbox=true&codePen=true--> |
| 46 | +```jsx |
| 47 | +import React from 'react'; |
| 48 | +import { Carousel } from 'uiw'; |
| 49 | + |
| 50 | +function Demo() { |
| 51 | + return ( |
| 52 | + <Carousel palyTime={1000} scrollTime={500}> |
| 53 | + <div style={{ height: '100%', background: '#1EABCD' }}> |
| 54 | + <span>1</span> |
| 55 | + </div> |
| 56 | + <div style={{ height: '100%', background: '#393b46' }}> |
| 57 | + <span>2</span> |
| 58 | + </div> |
| 59 | + <div style={{ height: '100%', background: '#008EF0' }}> |
| 60 | + <span>3</span> |
| 61 | + </div> |
| 62 | + <div style={{ height: '100%', background: '#393E48' }}> |
| 63 | + <span>4</span> |
| 64 | + </div> |
| 65 | + </Carousel> |
| 66 | + ); |
| 67 | +} |
| 68 | + |
| 69 | +ReactDOM.render(<Demo />, _mount_); |
| 70 | +``` |
| 71 | + |
| 72 | +## 切换到指定帧 |
| 73 | + |
| 74 | +手动切换到指定帧的位置 |
| 75 | + |
| 76 | +<!--rehype:bgWhite=true&codeSandbox=true&codePen=true--> |
| 77 | +```jsx |
| 78 | +import React from 'react'; |
| 79 | +import { Carousel } from 'uiw'; |
| 80 | + |
| 81 | +function Demo() { |
| 82 | + |
| 83 | + const ref=React.useRef() |
| 84 | + const [autoPlay,autoPlaySet]=React.useState(true) |
| 85 | + |
| 86 | + return ( |
| 87 | + <React.Fragment> |
| 88 | + <Carousel ref={ref} position={2} autoPlay={autoPlay}> |
| 89 | + <div style={{ height: '100%', background: '#1EABCD' }}> |
| 90 | + <span>1</span> |
| 91 | + </div> |
| 92 | + <div style={{ height: '100%', background: '#393b46' }}> |
| 93 | + <span>2</span> |
| 94 | + </div> |
| 95 | + <div style={{ height: '100%', background: '#008EF0' }}> |
| 96 | + <span>3</span> |
| 97 | + </div> |
| 98 | + <div style={{ height: '100%', background: '#393E48' }}> |
| 99 | + <span>4</span> |
| 100 | + </div> |
| 101 | + </Carousel> |
| 102 | + <button onClick={() => ref.current.gotoSlide(1)}>跳转</button> |
| 103 | + <button onClick={() => ref.current.prevSlide()}>上一张</button> |
| 104 | + <button onClick={() => ref.current.nextSlide()}>下一张</button> |
| 105 | + <button onClick={() =>autoPlaySet(autoPlay?false:true)}>{autoPlay?'暂停':'开始'}</button> |
| 106 | + </React.Fragment> |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | +ReactDOM.render(<Demo />, _mount_); |
| 111 | +``` |
| 112 | + |
| 113 | +## Props |
| 114 | + |
| 115 | +## API |
| 116 | + |
| 117 | +| 参数 | 说明 | 类型 | 默认值 | |
| 118 | +|--------- |-------- |--------- |-------- | |
| 119 | +| width | 宽度 | number | 400 | |
| 120 | +| height | 高度 | number | 200 | |
| 121 | +| position | 设置初始帧位置 | number | 0 | |
| 122 | +| palyTime | 每帧停留时间(ms) | number | 2000 | |
| 123 | +| scrollTime | 滚动动画的速度(ms) | number | 200 | |
| 124 | +| autoPlay | 是否自动播放 | boolean | true | |
| 125 | + |
| 126 | + |
| 127 | +### ref |
| 128 | + |
| 129 | +```ts |
| 130 | + // 跳转到指定帧 |
| 131 | + gotoSlide: (slideNumber: number) => void; |
| 132 | + // 上一针 |
| 133 | + prevSlide: () => void; |
| 134 | + // 下一帧 |
| 135 | + nextSlide: () => void; |
| 136 | + // 暂停播放 |
| 137 | + stopPlay: () => void; |
| 138 | +``` |
| 139 | + |
| 140 | + |
0 commit comments