forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
157 lines (138 loc) · 4.57 KB
/
index.html
File metadata and controls
157 lines (138 loc) · 4.57 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Parallax for Designers</title>
<link href='https://fonts.googleapis.com/css?family=Amatic+SC:400,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="page">
<div class="block one">
<div class="container">
<div class="moving left"></div>
<h2>Picture</h2>
<div class="moving right"></div>
</div>
</div>
<div class="block two">
<div class="container">
<div class="animation-container">
<div class="animate moving left">
<img src="img/shape_1.svg" />
</div>
<div class="animate moving down">
<img src="img/shape_2.svg" />
</div>
<div class="phone_container">
<img alt="phone" src="img/phone.png" class="phone"/>
<img alt="phone shadow" src="img/phone_shadow.png" class="shadow"/>
</div>
<div class="animate moving up">
<img src="img/shape_3.svg" />
</div>
<div class="animate moving right">
<img src="img/shape_4.svg" />
</div>
</div>
</div>
</div>
<div class="block three">
<div class="container">
<div class="animation-container">
<div class="moving left"></div>
<h2>Scrolling</h2>
<div class="moving right"></div>
</div>
<div class="block four">
<div class="container">
<div class="moving left"></div>
<h2>Picture</h2>
<div class="moving right"></div>
</div>
</div>
<div class="block five">
<div class="container">
<div class="moving left"></div>
<h2>Me</h2>
<div class="moving right"></div>
</div>
</div>
<div class="block six">
<div class="container">
<div class="moving left"></div>
<h2>Scrolling</h2>
<div class="moving right"></div>
</div>
</div>
</div>
</div>
</div>
</body>
<script>
function debounce(func, wait = 20, immediate = true) {
var timeout;
return function() {
var context = this, args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
};
function isVisible(element, onActive, onOff) {
const elementBox = element.getBoundingClientRect();
const viewHeight = Math.max(document.documentElement.clientHeight, window.innerHeight);
const slideInAt = viewHeight - (elementBox.height / 2);
const isHalfShown = slideInAt > elementBox.top;
const isNotScrolledPast = elementBox.bottom > 0;
if (isHalfShown && isNotScrolledPast) {
element.classList.add('parallax');
if (typeof onActive === 'function') {
onActive();
}
} else {
element.classList.remove('parallax');
if (typeof onOff === 'function') {
onOff();
}
}
}
function parallaxMe() {
const moveables = [].slice.call(document.querySelectorAll('.moving'));
moveables.forEach((image) => {
isVisible(image,
() => {
const parallaxEffect = `translate3d(0, 0, 0)`;
image.style.transform = parallaxEffect;
image.style.transition = `.5s cubic-bezier(.41,.07,.62,.91)`;
},
() => {
if (image.classList.contains('up')) {
const parallaxUp = 'translateX(-30px) translateY(-50px)'
image.style.transform = parallaxUp;
image.style.transition = `1s ease-in`;
} else if (image.classList.contains('right')) {
const parallaxRight = 'translateX(30px) translateY(50px)'
image.style.transform = parallaxRight;
image.style.transition = `1s ease-in`;
} else if (image.classList.contains('down')) {
const parallaxDown = 'translateX(-130px) translateY(-10px)'
image.style.transform = parallaxDown;
image.style.transition = `1s ease-in`;
} else if (image.classList.contains('left')) {
const parallaxLeft = 'translateX(130px) translateY(50px)'
image.style.transform = parallaxLeft;
image.style.transition = `1s ease-in`;
}
},
);
})
}
window.addEventListener('scroll', debounce(parallaxMe, 16));
</script>
</html>