Repository navigation
Expand file tree
/
Copy pathvideo-pause.js
More file actions
47 lines (43 loc) · 1.58 KB
/
Copy pathvideo-pause.js
File metadata and controls
47 lines (43 loc) · 1.58 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
window.addEventListener("DOMContentLoaded", (event) => {
let triggerSelector = "[data-video-pause='trigger']";
function pauseAllVideos() {
localStorage.setItem("videos-paused", "true");
$("body").addClass("videos-paused");
$(triggerSelector).addClass("is-paused");
$(triggerSelector).attr("aria-label", "Play Videos");
$("video").each(function () {
if ($(this).closest("[data-exclude-video]").length === 0 || !$(this)[0].hasAttribute("data-exclude-video")) {
$(this)[0].pause();
}
});
}
function playAllVideos() {
localStorage.setItem("videos-paused", "false");
$("body").removeClass("videos-paused");
$(triggerSelector).removeClass("is-paused");
$(triggerSelector).attr("aria-label", "Pause Videos");
$("video").each(function () {
if ($(this).closest("[data-exclude-video]").length === 0 || !$(this)[0].hasAttribute("data-exclude-video")) {
$(this)[0].muted = true;
$(this)[0].play();
}
});
}
const mediaQuery = window.matchMedia("(prefers-reduced-motion: reduce)");
function checkMotionPreference() {
mediaQuery.matches ? pauseAllVideos() : playAllVideos();
}
mediaQuery.addEventListener("change", () => {
checkMotionPreference();
});
if (localStorage.getItem("videos-paused") === "true") {
pauseAllVideos();
} else if (localStorage.getItem("videos-paused") === "false") {
playAllVideos();
} else {
checkMotionPreference();
}
$(document).on("click", triggerSelector, function () {
$("body").hasClass("videos-paused") ? playAllVideos() : pauseAllVideos();
});
});