index-main/assets/js/hero-video.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

2026-01-08 16:09:58 +01:00
export function playVideo() {
const playButton = document.querySelector('#hero-play-video');
if (!playButton) return;
2026-01-08 16:09:58 +01:00
const playerDiv = document.querySelector('#youtube-player');
if (!playerDiv) return;
2026-01-08 16:09:58 +01:00
// Charger l'API YouTube IFrame Player
const tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
document.head.appendChild(tag);
let ytReady = false;
window.onYouTubeIframeAPIReady = () => { ytReady = true; };
playButton.addEventListener('click', function () {
2026-01-08 16:09:58 +01:00
const extract = document.querySelector('.extract');
const videoFull = document.querySelector('.video-full');
2026-01-13 10:44:32 +01:00
const titleSmall = document.querySelector('.page-title-small');
if (extract) extract.style.display = 'none';
if (titleSmall) titleSmall.style.display = 'none';
2026-01-13 10:44:32 +01:00
2026-01-08 16:09:58 +01:00
if (videoFull) {
videoFull.style.display = 'block';
function createPlayer() {
new YT.Player('youtube-player', {
videoId: playerDiv.dataset.videoId,
width: '100%',
height: '100%',
playerVars: {
autoplay: 1,
rel: 0,
},
events: {
onReady: (e) => e.target.playVideo(),
},
});
}
2026-01-08 16:09:58 +01:00
if (ytReady) {
createPlayer();
} else {
window.onYouTubeIframeAPIReady = createPlayer;
2026-01-08 16:09:58 +01:00
}
}
});
}