Skip to content

Commit 89b910c

Browse files
committed
fix: allow setting max distance
1 parent 9a80433 commit 89b910c

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {Parallax} from '..';
2+
import {h} from '../../util';
3+
4+
const StoryParallax1 = () => {
5+
return (
6+
<div>
7+
<div style={{
8+
width: 300,
9+
height: 1e3,
10+
border: '1px solid tomato'
11+
}} />
12+
13+
<Parallax distance={300} onChange={(data) => console.log(data)}>{(state) =>
14+
<pre style={{border: '1px solid black', height: 300}}>{JSON.stringify(state, null, 4)}</pre>
15+
}</Parallax>
16+
17+
<div style={{
18+
width: 300,
19+
height: 1e3,
20+
border: '1px solid tomato'
21+
}} />
22+
</div>
23+
);
24+
};
25+
26+
export default StoryParallax1;

src/Parallax/__story__/story.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {storiesOf} from '@storybook/react';
33
import StoryParallax1 from './StoryParallax1';
44
import StoryParallax2 from './StoryParallax2';
55
import StoryParallax3 from './StoryParallax3';
6+
import StoryParallax4 from './StoryParallax4';
67

78
storiesOf('UI/Parallax', module)
89
.add('Basic example', () => <StoryParallax1 />)
910
.add('Card', () => <StoryParallax2 />)
10-
.add('With margin', () => <StoryParallax3 />);
11+
.add('With margin', () => <StoryParallax3 />)
12+
.add('Distance', () => <StoryParallax4 />);

src/Parallax/index.ts

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,42 @@ import {getElRect, getRootRect, TRect} from '../ViewportScrollSensor';
88
const zeros: TRect = [0, 0, 0, 0];
99

1010
export interface IParallaxProps extends IUniversalInterfaceProps<IParallaxState> {
11-
margin?: [number, number, number, number],
11+
distance?: number,
1212
throttle?: number,
13+
margin?: [number, number, number, number],
1314
onChange?: (IParallaxState) => void,
1415
}
1516

1617
export interface IParallaxState {
18+
distance: number,
19+
travelled: number,
1720
value: number,
1821
el: TRect,
1922
root: TRect,
2023
}
2124

2225
export class Parallax extends Component<IParallaxProps, IParallaxState> {
2326
static defaultProps = {
27+
distance: Infinity,
2428
margin: zeros,
2529
throttle: 50,
2630
};
2731

2832
el: HTMLElement;
2933
mounted: boolean = false;
30-
state: IParallaxState = {
31-
value: 0,
32-
el: zeros,
33-
root: zeros,
34-
};
34+
state: IParallaxState;
35+
36+
constructor (props, context) {
37+
super(props, context);
38+
39+
this.state = {
40+
distance: this.props.distance,
41+
travelled: 0,
42+
value: 0,
43+
el: zeros,
44+
root: zeros,
45+
};
46+
}
3547

3648
ref = (originalRef) => (el) => {
3749
this.el = el;
@@ -67,15 +79,15 @@ export class Parallax extends Component<IParallaxProps, IParallaxState> {
6779

6880
const rootHeight = root[3] - root[1];
6981
const elHeight = el[3] - el[1];
70-
const distance = rootHeight + elHeight;
71-
const remaining = distance - (el[3] - root[1]);
72-
const value = Math.max(0, Math.min(1, remaining / distance));
82+
const distance = Math.min(rootHeight + elHeight, this.props.distance);
83+
const travelled = root[3] - el[1];
84+
const value = Math.max(0, Math.min(1, travelled / distance));
7385

7486
if (value > 0 && value < 1) {
75-
this.change({value, el, root});
87+
this.change({distance, travelled, value, el, root});
7688
} else {
7789
if (this.state.value > 0 && this.state.value < 1) {
78-
this.change({value, el, root});
90+
this.change({distance, travelled, value, el, root});
7991
}
8092
}
8193
});

0 commit comments

Comments
 (0)