Created
May 6, 2023 22:56
-
-
Save JuanSebastianM/674b1139867974a779ba39eefb38e907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const musicGenres = ["Reggaeton", "Pop", "Country"]; | |
musicGenres.forEach(genre => { | |
const videoHtml = ` | |
<video class="carousel-video" preload muted playsinline data-genre=${genre}> | |
<source src=".../Muted_Video_${genre}.mp4" type="video/mp4"> | |
</video> | |
`; | |
// append each new video element to the carousel container | |
$(".videoCarouselContainer").append(videoHtml); | |
}); | |
const observer = new MutationObserver(mutationsList => { | |
for (const mutation of mutationsList) { | |
// save targeted element to avoid repeating `mutation.target` | |
const targetedVideoElement = mutation.target; | |
const isCurrentVideo = targetedVideoElement.getAttribute("aria-hidden") === "false"; | |
if (isCurrentVideo) { | |
// if video is visible, play it | |
targetedVideoElement.play(); | |
const currentVideoGenre = targetedVideoElement.getAttribute("data-genre"); | |
const popupVideoHtml = ` | |
<video controls> | |
<source src=".../Video_${currentVideoGenre}.mp4" type="video/mp4" /> | |
Your browser does not support the video tag. | |
</video> | |
`; | |
// append popup video to the modal container | |
$(".ModalContainer").html(popupVideoHtml); | |
} else { | |
// if video is not visible, pause and reset it | |
targetedVideoElement.pause(); | |
targetedVideoElement.currentTime = 0; | |
} | |
} | |
}); | |
const observerConfiguration = { | |
attributes: true, | |
attributeFilter: ["aria-hidden"], | |
}; | |
// get all muted video elements | |
const videoElements = document.querySelectorAll(".carousel-video"); | |
// observe them | |
videoElements.forEach(videoElement => { | |
observer.observe(videoElement, observerConfiguration); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment