Skip to content

Commit 3c5a487

Browse files
committed
add additional example parallax-test
1 parent e2c352a commit 3c5a487

File tree

6 files changed

+240
-0
lines changed

6 files changed

+240
-0
lines changed

examples/parallax-test/client.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import React from 'react';
2+
import ReactDOM from 'react-dom';
3+
import { ParallaxTest } from 'components';
4+
5+
import 'react-scroll-parallax/libs/ParallaxScrollListener';
6+
7+
const root = document.getElementById('root');
8+
9+
ReactDOM.render(<ParallaxTest />, root);
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import React from 'react';
2+
import { Parallax } from 'react-scroll-parallax';
3+
import style from './ParallaxTest.scss';
4+
5+
export default class ParallaxTest extends React.Component {
6+
7+
state = {
8+
amount: [1],
9+
offset: 50,
10+
slowerScrollRate: true,
11+
};
12+
13+
mapToParallax() {
14+
const offset = this.state.offset;
15+
const slowerScrollRate = this.state.slowerScrollRate;
16+
17+
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+
return (
22+
<Parallax
23+
key={i}
24+
tag="span"
25+
disabled={false}
26+
offsetYMax={offset * i + 'px'}
27+
offsetYMin={offset * -1 * i + 'px'}
28+
offsetXMax={0}
29+
offsetXMin={0}
30+
className="image"
31+
slowerScrollRate={slowerScrollRate}
32+
>
33+
{number}
34+
</Parallax>
35+
)
36+
});
37+
}
38+
39+
handleAdd = () => {
40+
const amount = [...this.state.amount, this.state.amount.length + 1];
41+
this.setState({
42+
amount,
43+
});
44+
};
45+
46+
handleRemove = () => {
47+
const amount = this.state.amount.slice(0, this.state.amount.length - 1);
48+
this.setState({
49+
amount,
50+
});
51+
};
52+
53+
increaseOffset = () => {
54+
const offset = this.state.offset + 50;
55+
// console.log('increase', offset);
56+
this.setState({
57+
offset,
58+
});
59+
};
60+
61+
decreaseOffset = () => {
62+
const offset = this.state.offset - 50 < 0 ? 0 : this.state.offset - 50;
63+
// console.log('decrease', offset);
64+
this.setState({
65+
offset,
66+
});
67+
};
68+
69+
toggleSpeed = () => {
70+
const slowerScrollRate = !this.state.slowerScrollRate;
71+
// console.log('slower scroll rate', slowerScrollRate);
72+
this.setState({
73+
slowerScrollRate,
74+
});
75+
};
76+
77+
render() {
78+
return (
79+
<div className="hello-world">
80+
<h1>
81+
{this.mapToParallax()}
82+
</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>
89+
</div>
90+
</div>
91+
);
92+
}
93+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
:global {
2+
body {
3+
background-color: #222;
4+
background-image:
5+
linear-gradient(0deg,
6+
transparent 24%,
7+
#333 25%,
8+
#333 26%,
9+
transparent 27%,
10+
transparent 74%,
11+
#333 75%,
12+
#333 76%,
13+
transparent 77%,
14+
transparent),
15+
linear-gradient(90deg, transparent 24%,
16+
#333 25%,
17+
#333 26%,
18+
transparent 27%,
19+
transparent 74%,
20+
#333 75%,
21+
#333 76%,
22+
transparent 77%,
23+
transparent);
24+
25+
background-size: 50px 50px;
26+
color: #342A45;
27+
font-family: 'Roboto Mono', sans-serif;
28+
font-style: normal;
29+
color: #eee;
30+
}
31+
32+
* {
33+
box-sizing: border-box;
34+
}
35+
36+
.hello-world {
37+
margin: 100vh 0;
38+
display: flex;
39+
flex-flow: row wrap;
40+
align-items: center;
41+
height: 300vh;
42+
}
43+
44+
h1 {
45+
width: 100%;
46+
font-size: 10vw;
47+
text-align: center;
48+
}
49+
50+
h1 > span {
51+
position: relative;
52+
display: inline-block;
53+
width: auto;
54+
}
55+
56+
.parallax-outer {
57+
position: relative;
58+
will-change: transform;
59+
}
60+
61+
.parallax-inner {
62+
position: relative;
63+
will-change: transform;
64+
background-color: rgba(255, 0, 0, 0.3);
65+
}
66+
67+
.parallax-outer::after {
68+
content: '';
69+
position: absolute;
70+
top: 0;
71+
left: 0;
72+
right: 0;
73+
bottom: 0;
74+
border: 2px solid orangered;
75+
}
76+
77+
.is-in-view .parallax-inner {
78+
background-color: rgba(0, 255, 0, 0.3);
79+
}
80+
81+
.parallax-inner::after {
82+
content: '';
83+
position: absolute;
84+
top: 0;
85+
left: 0;
86+
right: 0;
87+
bottom: 0;
88+
border: 2px solid green;
89+
}
90+
91+
.test {
92+
height: 400vh;
93+
}
94+
95+
.buttons {
96+
position: fixed;
97+
top: 0;
98+
left: 0;
99+
width: 100%;
100+
}
101+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export ParallaxTest from './ParallaxTest/ParallaxTest.js';

examples/parallax-test/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Example: React Scroll Parallax</title>
7+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/6.0.0/normalize.css">
8+
<link href="https://fonts.googleapis.com/css?family=Roboto+Mono:700" rel="stylesheet">
9+
</head>
10+
<body>
11+
<div id="root">$react</div>
12+
<script src="/static/bundle.js"></script>
13+
</body>
14+
</html>

examples/parallax-test/server.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import express from 'express';
4+
5+
import React from 'react';
6+
import ReactServer from 'react-dom/server';
7+
import { ParallaxTest } from 'components';
8+
9+
const app = express();
10+
11+
app.use('/static', express.static(path.resolve(__dirname, './dist')));
12+
13+
app.get('*', (req, res) => {
14+
const html = fs.readFileSync(path.resolve(__dirname, './index.html')).toString();
15+
const markup = ReactServer.renderToString(<ParallaxTest />);
16+
17+
res.send(html.replace('$react', markup));
18+
});
19+
20+
app.listen(3000, () => {
21+
console.log('Listening on: http://localhost:3000');
22+
});

0 commit comments

Comments
 (0)