@@ -2,37 +2,41 @@ import React from 'react';
22import { Parallax } from 'react-scroll-parallax' ;
33import style from './ParallaxTest.scss' ;
44
5+ const INC_AMOUNT = 10 ;
6+
57export default class ParallaxTest extends React . Component {
68
79 state = {
8- amount : [ 1 ] ,
9- offset : 50 ,
10- slowerScrollRate : true ,
10+ amount : new Array ( 10 ) . fill ( null ) . map ( ( x , i ) => i ) ,
11+ offsetY : INC_AMOUNT ,
12+ slowerScrollRate : false ,
13+ unitPercent : false ,
1114 } ;
1215
1316 mapToParallax ( ) {
14- const offset = this . state . offset ;
17+ const offsetY = this . state . offsetY ;
1518 const slowerScrollRate = this . state . slowerScrollRate ;
1619
1720 return this . state . amount . map ( ( number , i ) => {
18- const offsetYMin = offset * - 1 * i ;
19- const offsetYMax = offset * i ;
20- // console.log('min', offsetYMin, 'max', offsetYMax, 'slowerScrollRate', slowerScrollRate);
21+ const unit = this . state . unitPercent ? '%' : 'px' ;
22+ const offsetYMin = offsetY * - 1 * i + unit ;
23+ const offsetYMax = offsetY * i + unit ;
24+
2125 return (
2226 < Parallax
2327 key = { i }
2428 tag = "span"
2529 disabled = { false }
26- offsetYMax = { offset * i + 'px' }
27- offsetYMin = { offset * - 1 * i + 'px' }
30+ offsetYMax = { offsetYMax }
31+ offsetYMin = { offsetYMin }
2832 offsetXMax = { 0 }
2933 offsetXMin = { 0 }
30- className = "image"
34+ className = { style . item }
3135 slowerScrollRate = { slowerScrollRate }
3236 >
3337 { number }
3438 </ Parallax >
35- )
39+ ) ;
3640 } ) ;
3741 }
3842
@@ -50,42 +54,71 @@ export default class ParallaxTest extends React.Component {
5054 } ) ;
5155 } ;
5256
53- increaseOffset = ( ) => {
54- const offset = this . state . offset + 50 ;
55- // console.log('increase', offset);
57+ increaseOffsetY = ( ) => {
58+ const offsetY = this . state . offsetY + INC_AMOUNT ;
5659 this . setState ( {
57- offset ,
60+ offsetY ,
5861 } ) ;
5962 } ;
6063
61- decreaseOffset = ( ) => {
62- const offset = this . state . offset - 50 < 0 ? 0 : this . state . offset - 50 ;
63- // console.log('decrease', offset);
64+ decreaseOffsetY = ( ) => {
65+ const offsetY = this . state . offsetY - INC_AMOUNT < 0 ? 0 : this . state . offsetY - INC_AMOUNT ;
6466 this . setState ( {
65- offset ,
67+ offsetY ,
6668 } ) ;
6769 } ;
6870
6971 toggleSpeed = ( ) => {
7072 const slowerScrollRate = ! this . state . slowerScrollRate ;
71- // console.log('slower scroll rate', slowerScrollRate);
7273 this . setState ( {
7374 slowerScrollRate,
7475 } ) ;
7576 } ;
7677
78+ toggleValue = ( ) => {
79+ const unitPercent = ! this . state . unitPercent ;
80+ this . setState ( {
81+ unitPercent,
82+ } ) ;
83+ } ;
84+
7785 render ( ) {
7886 return (
79- < div className = "hello-world" >
80- < h1 >
87+ < div className = { style . parallaxTest } >
88+ < h1 className = { style . h1 } >
8189 { this . mapToParallax ( ) }
8290 </ h1 >
83- < div className = "buttons" >
84- < button onClick = { this . handleAdd } > Add</ button >
85- < button onClick = { this . handleRemove } > Remove</ button >
86- < button onClick = { this . increaseOffset } > Increase</ button >
87- < button onClick = { this . decreaseOffset } > Decrease</ button >
88- < button onClick = { this . toggleSpeed } > { this . state . slowerScrollRate ? 'Faster' : 'Slower' } </ button >
91+ < div className = { style . buttons } >
92+ < div className = { style . currentState } >
93+ < h4 >
94+ Parallax Elements:
95+ < span className = "value" > { this . state . amount . length } </ span >
96+ </ h4 >
97+ < button onClick = { this . handleAdd } > Add</ button >
98+ < button onClick = { this . handleRemove } > Remove</ button >
99+ </ div >
100+ < div className = { style . currentState } >
101+ < h4 >
102+ Y Offsets:
103+ < span className = "value" > { this . state . offsetY } </ span >
104+ </ h4 >
105+ < button onClick = { this . increaseOffsetY } > Increase</ button >
106+ < button onClick = { this . decreaseOffsetY } > Decrease</ button >
107+ </ div >
108+ < div className = { style . currentState } >
109+ < h4 >
110+ Speed:
111+ < span className = "value" > { this . state . slowerScrollRate ? 'Slower' : 'Faster' } </ span >
112+ </ h4 >
113+ < button onClick = { this . toggleSpeed } > { this . state . slowerScrollRate ? 'Faster' : 'Slower' } </ button >
114+ </ div >
115+ < div className = { style . currentState } >
116+ < h4 >
117+ Unit:
118+ < span className = "value" > { this . state . unitPercent ? 'Percent' : 'Pixels' } </ span >
119+ </ h4 >
120+ < button onClick = { this . toggleValue } > { this . state . unitPercent ? 'Pixels' : 'Percent' } </ button >
121+ </ div >
89122 </ div >
90123 </ div >
91124 ) ;
0 commit comments