-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path11.js
More file actions
76 lines (66 loc) · 2.45 KB
/
11.js
File metadata and controls
76 lines (66 loc) · 2.45 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
function playIt() {
const player = document.querySelector(".player");
const video = player.querySelector(".viewer");
const progress = player.querySelector(".progress");
const progressBar = player.querySelector(".progress__filled");
const toggle = player.querySelector(".toggle");
const skipButtons = player.querySelectorAll("[data-skip]");
const ranges = player.querySelectorAll(".player__slider");
const fullScreen = player.querySelector(".fullScreen");
function togglePlay() {
// Method 1
const method = video.paused ? "play" : "pause";
video[method]();
// Method 2
/*
if (video.paused) {
video.play();
} else {
video.pause();
}
*/
}
function updateButton() {
toggle.textContent = this.paused ? "►" : "❚ ❚";
}
function skip() {
video.currentTime += parseFloat(this.dataset.skip);
}
function handleRangeUpdate() {
video[this.name] = this.value;
}
function handleProgress() {
const percent = (video.currentTime / video.duration) * 100;
progressBar.style.flexBasis = `${percent}%`;
}
function scrub(e) {
video.currentTime = (e.offsetX / progress.offsetWidth) * video.duration;
}
function toggleFullScreen() {
if (buttonFullScreen) {
player.requestFullscreen();
} else {
document.exitFullscreen();
}
}
video.addEventListener("click", togglePlay);
video.addEventListener("play", updateButton);
video.addEventListener("pause", updateButton);
video.addEventListener("timeupdate", handleProgress);
toggle.addEventListener("click", togglePlay);
skipButtons.forEach((button) => button.addEventListener("click", skip));
ranges.forEach((range) =>
range.addEventListener("change", handleRangeUpdate)
);
let buttonFullScreen = true;
fullScreen.addEventListener("click", toggleFullScreen);
fullScreen.addEventListener("click", () => {
buttonFullScreen = buttonFullScreen == false ? true : false;
});
let mousedown = false;
progress.addEventListener("click", scrub);
progress.addEventListener("mousemove", (e) => mousedown && scrub(e));
progress.addEventListener("mousedown", () => (mousedown = true));
progress.addEventListener("mouseup", () => (mousedown = false));
}
window.addEventListener("DOMContentLoaded", playIt);