Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (55 loc) · 1.9 KB
/
Copy pathscript.js
File metadata and controls
61 lines (55 loc) · 1.9 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
// Save Our Wildlife — toggle button behaviour
//
// Requirement: button hides the video if it's visible/playing,
// or plays it if it's not already playing (and reveals it if hidden).
document.addEventListener("DOMContentLoaded", () => {
const video = document.getElementById("wildlife-video");
const videoFrame = video.closest(".video-frame");
const toggleBtn = document.getElementById("toggle-btn");
const iconPlay = toggleBtn.querySelector(".icon-play");
const iconHide = toggleBtn.querySelector(".icon-hide");
const btnLabel = toggleBtn.querySelector(".btn-label");
function setButtonState(state) {
// state: "play" or "hide"
if (state === "hide") {
iconPlay.hidden = false;
iconHide.hidden = true;
btnLabel.textContent = "Hide video";
toggleBtn.setAttribute("aria-pressed", "false");
} else {
iconPlay.hidden = true;
iconHide.hidden = false;
btnLabel.textContent = "Show & play video";
toggleBtn.setAttribute("aria-pressed", "true");
}
}
toggleBtn.addEventListener("click", () => {
const isHidden = videoFrame.classList.contains("is-hidden");
if (isHidden) {
// Video is hidden -> reveal it and play it
videoFrame.classList.remove("is-hidden");
video.play();
setButtonState("hide");
return;
}
if (video.paused) {
// Video is visible but not playing -> play it
video.play();
setButtonState("hide");
} else {
// Video is visible and playing -> hide it (and pause to save bandwidth)
video.pause();
videoFrame.classList.add("is-hidden");
setButtonState("play");
}
});
// Keep button label in sync if user pauses/plays via native controls
video.addEventListener("pause", () => {
if (!videoFrame.classList.contains("is-hidden")) {
setButtonState("play");
}
});
video.addEventListener("play", () => {
setButtonState("hide");
});
});