@@ -8,30 +8,42 @@ import {getElRect, getRootRect, TRect} from '../ViewportScrollSensor';
8
8
const zeros : TRect = [ 0 , 0 , 0 , 0 ] ;
9
9
10
10
export interface IParallaxProps extends IUniversalInterfaceProps < IParallaxState > {
11
- margin ?: [ number , number , number , number ] ,
11
+ distance ?: number ,
12
12
throttle ?: number ,
13
+ margin ?: [ number , number , number , number ] ,
13
14
onChange ?: ( IParallaxState ) => void ,
14
15
}
15
16
16
17
export interface IParallaxState {
18
+ distance : number ,
19
+ travelled : number ,
17
20
value : number ,
18
21
el : TRect ,
19
22
root : TRect ,
20
23
}
21
24
22
25
export class Parallax extends Component < IParallaxProps , IParallaxState > {
23
26
static defaultProps = {
27
+ distance : Infinity ,
24
28
margin : zeros ,
25
29
throttle : 50 ,
26
30
} ;
27
31
28
32
el : HTMLElement ;
29
33
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
+ }
35
47
36
48
ref = ( originalRef ) => ( el ) => {
37
49
this . el = el ;
@@ -67,15 +79,15 @@ export class Parallax extends Component<IParallaxProps, IParallaxState> {
67
79
68
80
const rootHeight = root [ 3 ] - root [ 1 ] ;
69
81
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 ) ) ;
73
85
74
86
if ( value > 0 && value < 1 ) {
75
- this . change ( { value, el, root} ) ;
87
+ this . change ( { distance , travelled , value, el, root} ) ;
76
88
} else {
77
89
if ( this . state . value > 0 && this . state . value < 1 ) {
78
- this . change ( { value, el, root} ) ;
90
+ this . change ( { distance , travelled , value, el, root} ) ;
79
91
}
80
92
}
81
93
} ) ;
0 commit comments