Skip to content

Commit 8706470

Browse files
authoredMar 22, 2022
feat(Carousel): 增加竖向滚动功能 (#704)
1 parent 665e18f commit 8706470

File tree

3 files changed

+52
-23
lines changed

3 files changed

+52
-23
lines changed
 

‎packages/react-carousel/README.md

+32-15
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,37 @@ import { Carousel } from 'uiw';
2424

2525
function Demo() {
2626
return (
27-
<Carousel>
28-
<div style={{ height: '100%', background: '#1EABCD' }}>
29-
<span>1</span>
30-
</div>
31-
<div style={{ height: '100%', background: '#393b46' }}>
32-
<span>2</span>
33-
</div>
34-
<div style={{ height: '100%', background: '#008EF0' }}>
35-
<span>3</span>
36-
</div>
37-
<div style={{ height: '100%', background: '#393E48' }}>
38-
<span>4</span>
39-
</div>
40-
</Carousel>
27+
<div style={{ display:'flex' }}>
28+
<Carousel>
29+
<div style={{ height: '100%', background: '#1EABCD' }}>
30+
<span>1</span>
31+
</div>
32+
<div style={{ height: '100%', background: '#393b46' }}>
33+
<span>2</span>
34+
</div>
35+
<div style={{ height: '100%', background: '#008EF0' }}>
36+
<span>3</span>
37+
</div>
38+
<div style={{ height: '100%', background: '#393E48' }}>
39+
<span>4</span>
40+
</div>
41+
</Carousel>
42+
<span style={{marginLeft:10}}/>
43+
<Carousel direction="vertical" >
44+
<div style={{ height: '100%', background: '#1EABCD' }}>
45+
<span>1</span>
46+
</div>
47+
<div style={{ height: '100%', background: '#393b46' }}>
48+
<span>2</span>
49+
</div>
50+
<div style={{ height: '100%', background: '#008EF0' }}>
51+
<span>3</span>
52+
</div>
53+
<div style={{ height: '100%', background: '#393E48' }}>
54+
<span>4</span>
55+
</div>
56+
</Carousel>
57+
</div>
4158
);
4259
}
4360

@@ -136,7 +153,7 @@ ReactDOM.render(<Demo />, _mount_);
136153
| autoPlay | 是否自动播放 | boolean | true |
137154
| afterChange | 切换面板前的回调 | (current) => void | - |
138155
| beforeChange | 切换面板后的回调 | (current) => void | - |
139-
156+
| direction | 滚动方位`horizontal`横向,`vertical`竖向 | horizontal \| vertical | horizontal |
140157

141158
### ref
142159

‎packages/react-carousel/src/index.tsx

+20-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface CarouselProps extends IProps, HTMLDivProps {
99
palyTime?: number;
1010
scrollTime?: number;
1111
autoPlay?: boolean;
12+
direction?: 'horizontal' | 'vertical';
1213
afterChange?: (current: number) => void;
1314
beforeChange?: (current: number) => void;
1415
}
@@ -23,6 +24,7 @@ export interface CarouselRef {
2324
function Carousel(props: CarouselProps, ref: React.ForwardedRef<CarouselRef>) {
2425
const {
2526
position = 0,
27+
direction = 'horizontal',
2628
width = 400,
2729
height = 200,
2830
palyTime = 2000,
@@ -110,15 +112,28 @@ function Carousel(props: CarouselProps, ref: React.ForwardedRef<CarouselRef>) {
110112
return <div style={{ width, height, ...style }}>{child}</div>;
111113
});
112114

115+
const innerStyle = useMemo(() => {
116+
const style = { transform: '', display: '' };
117+
switch (direction) {
118+
case 'horizontal':
119+
style.transform = `translate3d(${-(currentPosition * width)}px, 0px, 0px)`;
120+
style.display = 'flex';
121+
break;
122+
case 'vertical':
123+
style.transform = `translate3d(0px, ${-(currentPosition * height)}px, 0px)`;
124+
style.display = 'block';
125+
break;
126+
default:
127+
break;
128+
}
129+
return style;
130+
}, [direction, currentPosition, width, height]);
131+
113132
return (
114133
<div className={cls} style={{ width, height }}>
115134
<div
116135
className={`${cls}-content`}
117-
style={{
118-
width: width * childCount,
119-
transform: `translate3d(${-(currentPosition * width)}px, 0px, 0px)`,
120-
transition: transitionInner,
121-
}}
136+
style={{ width: width * childCount, transition: transitionInner, ...innerStyle }}
122137
>
123138
{childrens}
124139
<div style={{ width, height, ...style }}>{childrens?.[0]}</div>

‎packages/react-carousel/src/style/index.less

-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55

66
&-content {
77
height: 200px;
8-
// transform: translate3d();
9-
display: flex;
10-
flex-direction: row;
118
transition: 0.6s ease-in-out;
129
}
1310
}

0 commit comments

Comments
 (0)
Please sign in to comment.