-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
42 lines (37 loc) · 1.1 KB
/
script.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
const videoPlayer = document.getElementById('videoPlayer');
const playPauseButton = document.querySelector('.play-pause-button');
let isPaused = false;
let wasPausedBeforeBlur = false;
videoPlayer.addEventListener('ended', () => {
videoPlayer.currentTime = 0;
videoPlayer.pause();
playPauseButton.classList.add('video-ended');
});
playPauseButton.addEventListener('click', () => {
if (videoPlayer.paused) {
videoPlayer.play();
playPauseButton.parentElement.classList.add('video-playing');
playPauseButton.classList.remove('video-ended');
} else {
videoPlayer.pause();
playPauseButton.parentElement.classList.remove('video-playing');
}
});
function handleVisibilityChange() {
if (document.hidden) {
wasPausedBeforeBlur = videoPlayer.paused;
videoPlayer.pause();
isPaused = true;
} else if (isPaused) {
if (!wasPausedBeforeBlur) {
videoPlayer.play();
}
isPaused = false;
}
}
document.addEventListener('visibilitychange', handleVisibilityChange);
window.addEventListener('beforeunload', () => {
if (!videoPlayer.paused) {
videoPlayer.pause();
}
});