@@ -8,30 +8,42 @@ import {getElRect, getRootRect, TRect} from '../ViewportScrollSensor';
88const zeros : TRect = [ 0 , 0 , 0 , 0 ] ;
99
1010export 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
1617export interface IParallaxState {
18+ distance : number ,
19+ travelled : number ,
1720 value : number ,
1821 el : TRect ,
1922 root : TRect ,
2023}
2124
2225export 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