-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathparallax__demo.html
More file actions
94 lines (86 loc) · 2.22 KB
/
Copy pathparallax__demo.html
File metadata and controls
94 lines (86 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="./style.css" />
<title>VanillaScroll</title>
</head>
<style>
body {
margin: 0;
height: 100vh;
font-size: 5em;
font-family: "Montserrat";
font-weight: bold;
overflow-x: hidden;
}
section {
height: 100%;
overflow: hidden;
overflow-x: hidden;
}
ul {
list-style-type: none;
padding: 0;
margin: 0;
display: grid;
grid-template-columns: repeat(3, auto);
justify-content: center;
align-items: middle;
place-items: center;
height: 100vh;
}
span {
width: 110px;
height: 110px;
position: absolute;
bottom: 1rem;
left: 1rem;
background: black;
border-radius: 50%;
}
</style>
<body>
<section>
<ul>
<li class="scroll" data-rate="-2">Par</li>
<li>ral</li>
<li
class="scroll"
data-direction="horizontal"
data-rateY="2"
data-rateX="2"
>
lax
</li>
</ul>
<span
class="scroll"
data-direction="horizontal"
data-rateY="1"
data-rateX="2"
></span>
</section>
<section></section>
</body>
<script>
window.addEventListener("scroll", function (e) {
const target = document.querySelectorAll(".scroll");
let length = target.length; //length is the number of elements in the array
for (let index = 0; index < length; index++) {
let position = window.pageYOffset * target[index].dataset.rate;
if (target[index].dataset.direction === "horizontal") {
positionX = window.pageYOffset * target[index].dataset.ratex;
positionY = window.pageYOffset * target[index].dataset.ratey;
target[index].style.transform =
"translate3d( " + positionX + "px," + positionY + "px, 0)";
} else {
target[index].style.transform =
"translate3d(0, " + position + "px, 0)";
}
}
});
</script>
</html>