-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
327 lines (271 loc) · 11.3 KB
/
index.html
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Scroll-driven scroll-snapping animations in CSS</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="stylesheet" href="./src/resets/theme.css" />
<link rel="stylesheet" href="./src/resets/base.css" />
<link rel="stylesheet" href="./src/resets/rhythm.css" />
<link rel="stylesheet" href="./src/resets/layout.css" />
<link rel="stylesheet" href="./src/page.css" />
<link rel="stylesheet" href="./src/sections.css" />
<link href="https://fonts.googleapis.com/css2?family=Fraunces:ital,opsz,wght@0,9..144,100..400;1,9..144,100..400&family=Noto+Sans+Display:[email protected]&display=swap" rel="stylesheet">
<style>
@layer base, rhythm, layout, default;
html {
/* Create a snapping rule on the html element */
scroll-snap-type: y mandatory;
/* Create a timeline scope, so we can target any element on the page */
timeline-scope: --section, --main, --site-header;
}
/* We'll want to match these colors for the best melty effect */
/* But play around with them (and add a color to section) to see happens */
body,
.content {
background-color: var(--color-background, black);
}
main {
view-timeline: --main;
}
.section {
/* Creating a snapping rule on the section element */
scroll-snap-align: start;
scroll-snap-stop: always;
/* Attach the timeline to the section element*/
view-timeline: --section;
/* Set each section to the full dynamic height of the viewport */
height: 100dvh;
}
.content {
/* Fix the content, so it doesn't scroll with the section */
overflow: hidden;
position: fixed;
inset: 0;
/* Animate the content based on the section scrolling */
--contrast: 4;
--blur: 0.5rem;
animation: blink ease-in-out both;
animation-timeline: --section;
}
@keyframes blink {
0%,
100% {
filter: blur(var(--blur)) contrast(var(--contrast));
opacity: 0;
visibility: hidden;
}
50% {
filter: blur(0) contrast(1);
opacity: 1;
visibility: visible;
}
}
/*
This is a bit of a hack to get the indicator to work because I'm lazy.
We're translating the dot from the top to the bottom of its parent,
using the browser scroll position as the animation timeline.
It's not really matched up to the scrolling sections, only appears to be.
*/
.indicator::before {
animation: indicate linear both;
animation-timeline: --main;
animation-range: contain;
}
/* And we're manually setting the colors because see: lazy comment above */
@keyframes indicate {
0% {
--color-indicator: var(--color-primary);
transform: translateY(0);
}
25% {
--color-indicator: var(--color-yellow);
}
50% {
--color-indicator: var(--color-secondary);
}
75% {
--color-indicator: var(--color-red);
}
100% {
--color-indicator: var(--color-purple);
transform: translateY(calc(var(--indicator-total-height) - var(--indicator-size)));
}
}
/* Remove gradient indicator on scroll-to-end (visible on small screens */
.site-header label:last-of-type {
view-timeline: --site-header inline;
}
.site-header::after {
animation: fade-scroll ease-in-out both;
animation-timeline: --site-header;
animation-range: entry-crossing;
}
@keyframes fade-scroll {
0% {
opacity: 1;
}
100% {
opacity: 0;
}
}
/* Change animation based on radio checked */
body:has([value="horizontal-scroll"]:checked) .content {
/*
The only reason we're repeat these two properties instead of simply
setting `animation-name` is so the polyfill will pick them up
They're flaky though and tend to get stuck. You might need to
refresh the page and select an option before scrolling
*/
animation: horizontal-scroll ease-in-out both;
animation-timeline: --section;
}
body:has([value="backwards-scroll"]:checked) .content {
animation: backwards-scroll ease-in-out both;
animation-timeline: --section;
}
body:has([value="zoom-scroll"]:checked) .content {
animation: zoom-scroll ease-in-out both;
animation-timeline: --section;
}
/* Alternative animations */
/* Very cool, try it */
@keyframes horizontal-scroll {
0% {
transform: translate3d(100%, 0%, 0);
}
50% {
transform: none;
}
100% {
transform: translate3d(-100%, 0%, 0);
}
}
/* Befuddling, try it */
@keyframes backwards-scroll {
0% {
transform: translate3d(0%, -100%, 0);
}
50% {
transform: none;
}
100% {
transform: translate3d(0%, 100%, 0);
}
}
/* WIP */
@keyframes zoom-scroll {
0% {
filter: blur(5rem);
transform: scale(0);
opacity: 0;
visibility: hidden;
}
50% {
filter: blur(0);
transform: none;
opacity: 1;
visibility: visible;
}
100% {
filter: blur(3rem);
transform: scale(1.5);
opacity: 0;
visibility: hidden;
}
}
</style>
</head>
<body>
<header class="site-header">
<h1 class="sr-only">Scrollnapping animations</h1>
<!-- Because Chrome crashes if an overflow is set on the fieldset while a named timeline is on a child element -->
<!-- ¯\_(ツ)_/¯ -->
<div class="fieldset-wrapper">
<fieldset>
<legend class="sr-only">Effects</legend>
<!-- radio buttons for each of the @keyframes above -->
<input type="radio" id="blink-effect" name="effect" value="blink" checked class="sr-only">
<label for="blink-effect">Blink</label>
<input type="radio" id="horizontal-scroll-effect" name="effect" value="horizontal-scroll" class="sr-only">
<label for="horizontal-scroll-effect">Horizontal scroll</label>
<input type="radio" id="backwards-scroll-effect" name="effect" value="backwards-scroll" class="sr-only">
<label for="backwards-scroll-effect">Backwards scroll</label>
<input type="radio" id="zoom-scroll-effect" name="effect" value="zoom-scroll" class="sr-only">
<label for="zoom-scroll-effect">Zoom scroll</label>
</fieldset>
</div>
<nav>
<ul class="indicator">
<li><a href="#snapping"><span class="sr-only">Snapping</span></a></li>
<li><a href="#scrolling"><span class="sr-only">Scrolling</span></a></li>
<li><a href="#layout"><span class="sr-only">Layout</span></a></li>
<li><a href="#transition"><span class="sr-only">Transition</span></a></li>
<li><a href="#caveats"><span class="sr-only">Caveats</span></a></li>
</ul>
</nav>
</header>
<main>
<section id="snapping" class="section">
<div class="content">
<h2><strong>First</strong>, we set up the <em>snapping</em> points</h2>
<div class="text">
<img src="src/img/flower-white.png" alt="">
<p>We set the scrolling element, in this case our <code class="selector">HTML</code> element to forcibly snap to the Y axis by using <code class="property">scroll-snap-type: y mandatory</code>.</p>
<p>And then we set <code class="selector">section</code> as the snapping elements by using <code class="property">scroll-snap-align: start</code>.</p>
</div>
</div>
</section>
<section id="scrolling" class="section">
<div class="content">
<h2><strong>Next</strong>, we set up the <em>scrolling</em> animation</h2>
<div class="text">
<img src="src/img/flower-yellow.png" alt="">
<p>We set the scrolling element, in this case our <code class="selector">section</code> elements, to track their view() position inside the browser using <code class="property">view-timeline: --section;</code>. We had previous set the <code class="property">timeline-scope: --section</code> up in our <code class="selector">HTML</code> element, so we can access it from anywhere in the document.</p>
<p>We animate the <code class="selector">.content</code> children using <code class="property">animation-timeline: --section;</code>. The <code class="selector">.content</code> element will now animate based on its parent <code class="selector">section</code>'s position. This is important due to how we're handling the layout in the next section.</p>
</div>
</div>
</section>
<section id="layout" class="section">
<div class="content">
<h2><strong>Then</strong>, we position a <em>fixed</em> layout</h2>
<div class="text">
<img src="src/img/flower-blue.png" alt="">
<p>We set the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, so they're removed from the normal document flow and stack on top of each other. We give them a solid background, so we can see only one at a time.</p>
<p>Their parent <code class="selector">section</code>s are positioned as normal in the layer below, taking up space, scroll-snapping, and powering the <code class="property">animation-timeline</code>.</p>
</div>
</div>
</section>
<section id="transition" class="section">
<div class="content">
<h2><strong>Finally</strong>, we create the <em>transition</em> effects</h2>
<div class="text">
<img src="src/img/flower-red.png" alt="">
<p>By setting the <code class="selector">.content</code> elements to <code class="property">position: fixed</code>, we can now transition between them without a visible scrolling movement.</p>
<p>We create a normal <code class="selector">@keyframe</code> animation to our liking to transition between them. Check the navigation menu to see different effects.</p>
</div>
</div>
</section>
<section id="caveats" class="section">
<div class="content">
<h2><strong>Caveats</strong></h2>
<div class="text">
<img src="src/img/flower-purple.png" alt="">
<ul>
<li>Scrolling animations are not currently available in Firefox. This demo is using a polyfill.</li>
<li>This layout is fragile due to the use of <code class="property">position: fixed</code>. You need to carefully manage stacking contexts.</li>
<li>Snapping points have their own caveats, such as content taller than the viewport becoming inaccessible, along with the general annoyance of scrolljacking.</li>
<li>The <code class="selector">blink</code> effect uses the <code class="property">contrast()</code> filter, which modifies the colors of the entire section. Thus, the background is set to black (or white), ensuring that it appears unchanged during transitions due to already being at maximum contrast.</li>
</ul>
</div>
</div>
</section>
</main>
<footer>
<p>That's it <span class="emoji">🌸</span></p>
</footer>
<script src="https://flackr.github.io/scroll-timeline/dist/scroll-timeline.js"></script>
</body>
</html>