-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.html
108 lines (89 loc) · 2.11 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
<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
margin: 0;
font-family: -apple-system, sans-serif;
color: #1d1d1f;
}
h1 {
font-size: 40px;
margin: 0 0 10px;
}
p {
font-size: 22px;
}
.hero {
padding: 40px;
text-align: center;
}
.hero-sequence {
height: 430vh;
}
.sticky-element {
position: sticky;
top: 0;
height: 100vh;
min-height: 1033px;
overflow: hidden;
display: grid;
place-items: center;
}
.sequence-element {
width: 1336px;
height: 786px;
}
footer {
background: #f5f5f7;
font-weight: 700;
font-size: 40px;
height: 400px;
display: grid;
place-items: center;
}
</style>
<title>Apple Sequence Animation</title>
<div class="hero">
<h1>Apple Sequence Animation</h1>
<p>Scroll down to admire the magic.</p>
</div>
<div class="hero-sequence">
<div class="sticky-element">
<div class="sequence-element">
<canvas width="1336" height="786"></canvas>
</div>
</div>
</div>
<footer>
You are awesome!
</footer>
<script>
const canvas = document.querySelector('canvas')
const ctx = canvas.getContext('2d')
const heroSequence = document.querySelector('.hero-sequence')
const images = []
const frameCount = 87
const prepareImages = () => {
for (var i = 0; i < frameCount; i++) {
const image = new Image()
image.src = `./images/${i}.jpg`
images.push(image)
if (i === 0) {
images[i].onload = () => drawImage(0)
}
}
}
const drawImage = frameIndex => {
ctx.drawImage(images[frameIndex], 0, 0)
}
prepareImages()
window.addEventListener('scroll', () => {
const scrollTop = document.documentElement.scrollTop - heroSequence.offsetTop
const maxScrollTop = heroSequence.scrollHeight - window.innerHeight
const scrollFraction = scrollTop / maxScrollTop
const frameIndex = Math.max(0, Math.min(frameCount - 1, Math.ceil(scrollFraction * frameCount)))
images[frameIndex].onload = () => drawImage(frameIndex)
requestAnimationFrame(() => drawImage(frameIndex))
})
</script>