-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptimised.html
More file actions
125 lines (114 loc) · 3.81 KB
/
Copy pathoptimised.html
File metadata and controls
125 lines (114 loc) · 3.81 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Optimised scroll animations — scroll-jank-profiler demo</title>
<style>
/*
The same four effects as janky.html, rebuilt so every animated property is
one the compositor can handle on its own thread.
*/
* { box-sizing: border-box; }
body { margin: 0; font: 16px/1.6 system-ui, sans-serif; background: #101216; color: #e8eaed; }
header { padding: 4rem 2rem; text-align: center; }
h1 { margin: 0 0 .5rem; font-size: clamp(1.6rem, 4vw, 2.6rem); }
p.lede { color: #9aa2ad; max-width: 40rem; margin: 0 auto; }
section { min-height: 100vh; padding: 4rem 2rem; border-top: 1px solid #23262d; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 1rem; }
/* scale() instead of width/height/margin. */
@keyframes grow-transform {
from { transform: scale(1) translateX(0); }
to { transform: scale(2.6) translateX(46px); }
}
.grower {
width: 120px; height: 120px;
transform-origin: top left;
background: linear-gradient(135deg, #4cc38a, #7aa2f7);
border-radius: 18px;
animation: grow-transform linear both;
animation-timeline: view();
animation-range: entry 0% cover 60%;
}
/* The shadow is carried by a pseudo-element and cross-faded with opacity. */
.grower::after {
content: "";
position: absolute;
inset: 0;
border-radius: 18px;
box-shadow: 0 40px 90px rgba(0, 0, 0, .7);
opacity: 0;
animation: fade-in linear both;
animation-timeline: view();
animation-range: entry 0% cover 60%;
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
/* translate() instead of top/left. */
@keyframes slide-transform {
from { transform: translate3d(0, 0, 0); }
to { transform: translate3d(60px, 240px, 0); }
}
.slider {
width: 140px; height: 140px;
background: #7aa2f7;
border-radius: 12px;
animation: slide-transform linear both;
animation-timeline: scroll(root block);
}
/* An oversized child is translated instead of moving background-position. */
.panner { height: 300px; overflow: hidden; }
.panner > .strip {
height: 1500px;
background-image: repeating-linear-gradient(45deg, #2a2e35 0 20px, #1a1d22 20px 40px);
animation: pan-transform linear both;
animation-timeline: view();
}
@keyframes pan-transform {
from { transform: translate3d(0, 0, 0); }
to { transform: translate3d(0, -1200px, 0); }
}
/*
No blanket will-change. Only the handful of elements that actually animate
are promoted, and they are promoted by the animation itself.
*/
.tile { height: 120px; border-radius: 10px; background: #1c1f25; }
</style>
</head>
<body>
<header>
<h1>Optimised scroll animations</h1>
<p class="lede">The same effects as the janky demo, expressed entirely with <code>transform</code>
and <code>opacity</code>, with no scroll listener and no blanket layer promotion.</p>
</header>
<section>
<h2>Growing box (transform: scale)</h2>
<div class="grower"></div>
</section>
<section>
<h2>Sliding box (transform: translate3d)</h2>
<div class="slider"></div>
</section>
<section>
<h2>Panning strip (transform on an oversized child)</h2>
<div class="panner"><div class="strip"></div></div>
</section>
<section>
<h2>Plain tiles</h2>
<div class="grid" id="tiles"></div>
</section>
<section>
<h2>End of the page</h2>
<p>Compare against <a href="janky.html">janky.html</a>.</p>
</section>
<script>
const tiles = document.getElementById('tiles');
const fragment = document.createDocumentFragment();
for (let i = 0; i < 120; i += 1) {
const tile = document.createElement('div');
tile.className = 'tile';
fragment.append(tile);
}
tiles.append(fragment);
</script>
</body>
</html>