-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
46 lines (39 loc) · 1.3 KB
/
Copy pathscript.js
File metadata and controls
46 lines (39 loc) · 1.3 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
// Carrega a API do YouTube
let tag = document.createElement("script");
tag.src = "https://www.youtube.com/iframe_api";
let firstScriptTag = document.getElementsByTagName("script")[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
let players = [];
let currentIndex = 0;
let videoContainers;
function onYouTubeIframeAPIReady() {
videoContainers = document.querySelectorAll(".video-container");
videoContainers.forEach((container, index) => {
const iframe = container.querySelector("iframe");
players[index] = new YT.Player(iframe, {
events: {
onReady: (event) => {
if (index === 0) {
event.target.playVideo(); // autoplay primeiro vídeo
}
},
onStateChange: (event) => onPlayerStateChange(event, index),
},
});
});
}
function onPlayerStateChange(event, index) {
if (event.data === YT.PlayerState.ENDED) {
goToNextVideo();
}
}
function goToNextVideo() {
// Parar vídeo atual
players[currentIndex].stopVideo();
videoContainers[currentIndex].classList.remove("active");
// Atualizar índice
currentIndex = (currentIndex + 1) % players.length;
// Ativar próximo
videoContainers[currentIndex].classList.add("active");
players[currentIndex].playVideo();
}