-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathscripts.js
50 lines (46 loc) · 2.09 KB
/
scripts.js
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
"use strict";
(() => {
const player = document.querySelector(".player"),
video = player.querySelector(".viewer"),
progress = player.querySelector(".progress"),
progressBar = player.querySelector(".progress__filled"),
toggle = player.querySelector(".toggle"),
skipButtons = player.querySelectorAll("[data-skip]"),
ranges = player.querySelectorAll(".player__slider");
const togglePlay = () => video[video.paused ? "play" : "pause"](),
updateButton = () => toggle.textContent = video.paused ? "►" : "❚ ❚",
handleProgress = () =>
progressBar.style.flexBasis = `${video.currentTime / video.duration * 100}%`,
scrub = e =>
video.currentTime = e.offsetX / progress.offsetWidth * video.duration,
progressMoved = e => mousedown && scrub(e),
progressUp = e => mousedown = false,
progressDown = e => mousedown = true,
bClick = b => video.currentTime += parseFloat(b.dataset.skip),
updateRange = (range, e) => video[range.name] = range.value;
let mousedown = false;
const events = [
{ event: "click", handler: togglePlay, target: video },
{ event: "play", handler: updateButton, target: video },
{ event: "pause", handler: updateButton, target: video },
{ event: "timeupdate", handler: handleProgress, target: video },
{ event: "click", handler: togglePlay, target: toggle },
{ event: "click", handler: scrub, target: progress },
{ event: "mousedown", handler: progressDown, target: progress },
{ event: "mousemove", handler: progressMoved, target: progress },
{ event: "mouseup", handler: progressUp, target: progress },
{ event: "click", handler: bClick, target: skipButtons },
{ event: ["change", "mousemove"], handler: updateRange, target: ranges }
];
events.forEach(
({ event: e, handler: h, target: t }) =>
(t instanceof NodeList
? t.forEach((el, i) => {
el.addEventListener(
typeof e === "string" ? e : e[i],
typeof h === "function" ? h.bind(null, el) : h[i].bind(null, el)
);
})
: t.addEventListener(e, h))
);
})();