-
Notifications
You must be signed in to change notification settings - Fork 0
/
video-iframe.php
86 lines (74 loc) · 2.07 KB
/
video-iframe.php
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
77
78
79
80
81
82
83
84
85
86
<div class="home-intro__video">
<div data-video="video_id" id="player"></div>
</div>
<script>
export default function introVideo() {}
const playerEl = document.querySelector("#player");
if (playerEl) {
let player;
const videoCode = playerEl.getAttribute("data-video");
let videoReady = false;
window.YT.ready(function onYouTubeIframeAPIReady() {
player = new YT.Player("player", {
videoId: videoCode,
playerVars: {
playsinline: 1,
autoplay: 1,
mute: 1,
controls: 0,
showinfo: 0,
rel: 0,
loop: 1,
playlist: videoCode,
},
events: {
onReady: onPlayerReady,
onStateChange: onPlayerStateChange,
},
});
videoReady = true;
});
function onPlayerReady(event) {
setTimeout(function() {
event.target.playVideo();
}, 100);
}
let isPlaying = false;
function onPlayerStateChange(event) {
if (event.data == YT.PlayerState.ENDED) {
// console.log("Video Ended");
isPlaying = false;
document.documentElement.classList.remove("video-playing");
}
if (event.data == YT.PlayerState.PLAYING) {
// console.log("Video Playing");
isPlaying = true;
document.documentElement.classList.add("video-playing");
}
if (event.data == YT.PlayerState.PAUSED) {
// console.log("Video Paused");
isPlaying = false;
}
if (event.data == YT.PlayerState.BUFFERING) {
// console.log("Video Buffering");
}
if (event.data == YT.PlayerState.CUED) {
// console.log("Video Cued");
document.documentElement.classList.remove("video-playing");
isPlaying = false;
}
}
// if (window.innerWidth > 750) {
// onYouTubeIframeAPIReady();
// }
window.addEventListener("resize", () => {
if (window.innerWidth > 750 && !videoReady) {
onYouTubeIframeAPIReady();
}
if (isPlaying) {
player.stopVideo();
}
});
}
}
</script>