Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
124 lines (110 loc) · 4 KB
/
Copy pathscript.js
File metadata and controls
124 lines (110 loc) · 4 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
const hero = document.querySelector('.hero');
const video = document.querySelector('#demo-video');
const playPrompt = document.querySelector('#play-prompt');
const soundToggle = document.querySelector('#sound-toggle');
const volumeControl = document.querySelector('#volume-control');
const reduceMotion = window.matchMedia('(prefers-reduced-motion: reduce)');
if (!window.location.hash) {
history.scrollRestoration = 'manual';
window.scrollTo({ top:0, left:0, behavior:'instant' });
}
video.muted = false;
video.defaultMuted = false;
function setVolume(value) {
// Some mobile browsers leave volume control to the device buttons.
try { video.volume = value; } catch (_) { /* Keep playback available. */ }
}
setVolume(Number(volumeControl.value) / 100);
function updateSoundButton() {
const on = !video.muted && video.volume > 0;
soundToggle.textContent = on ? 'Sound on' : 'Tap for sound';
soundToggle.setAttribute('aria-label', on ? 'Turn sound off' : 'Turn sound on');
soundToggle.setAttribute('aria-pressed', String(on));
}
function muteOnScroll() {
const scrolled = window.scrollY > 0;
document.body.classList.toggle('scrolled-away', scrolled);
if (scrolled && !video.muted) {
video.muted = true;
updateSoundButton();
}
}
function enableSound() {
if (window.scrollY > 0) return;
setVolume(Number(volumeControl.value) / 100 || 0.18);
video.muted = false;
updateSoundButton();
video.play().catch(showPlayPrompt);
}
video.addEventListener('click', enableSound);
function updateHero() {
document.body.classList.toggle('has-scrolled', window.scrollY > 0);
muteOnScroll();
if (reduceMotion.matches) {
document.documentElement.style.setProperty('--reveal', '1');
document.documentElement.style.setProperty('--rise', '1');
document.documentElement.style.setProperty('--slide', '0');
return;
}
const distance = Math.max(1, hero.offsetHeight - window.innerHeight);
const progress = Math.min(1, Math.max(0, -hero.getBoundingClientRect().top / distance));
// Reveal the title while the sticky hero leaves with the section, avoiding a blank viewport.
const rise = Math.min(1, progress / 0.38);
const reveal = Math.min(1, Math.max(0, (progress - 0.12) / 0.22));
const slide = 0;
document.documentElement.style.setProperty('--reveal', reveal.toFixed(3));
document.documentElement.style.setProperty('--rise', rise.toFixed(3));
document.documentElement.style.setProperty('--slide', slide.toFixed(3));
}
let frame = 0;
window.addEventListener('scroll', () => {
if (!frame) frame = requestAnimationFrame(() => { updateHero(); frame = 0; });
}, { passive:true });
window.addEventListener('resize', updateHero);
reduceMotion.addEventListener('change', updateHero);
updateHero();
soundToggle.addEventListener('click', () => {
if (window.scrollY > 0) return;
if (video.volume === 0) {
volumeControl.value = '18';
setVolume(0.18);
}
video.muted = !video.muted;
updateSoundButton();
if (!video.muted) video.play().catch(showPlayPrompt);
});
volumeControl.addEventListener('input', () => {
setVolume(Number(volumeControl.value) / 100);
if (window.scrollY === 0) video.muted = video.volume === 0;
updateSoundButton();
if (!video.muted) video.play().catch(showPlayPrompt);
});
function showPlayPrompt() {
playPrompt.hidden = false;
}
video.addEventListener('playing', () => {
playPrompt.hidden = true;
updateSoundButton();
});
video.addEventListener('error', showPlayPrompt);
// A stalled download is buffering, not evidence that autoplay was blocked.
playPrompt.addEventListener('click', () => {
if (video.error) video.load();
if (window.scrollY === 0) {
enableSound();
}
video.play().catch(showPlayPrompt);
});
async function startPlayback() {
if (window.scrollY > 0) video.muted = true;
try {
await video.play();
updateSoundButton();
} catch (_) {
// Keep the video moving if the browser disallows autoplay with audio.
video.muted = true;
updateSoundButton();
try { await video.play(); } catch (_) { showPlayPrompt(); }
}
}
startPlayback();