Created
August 15, 2022 14:55
-
-
Save aslamdoctor/8d31a361d89aff3eabe8832cfad59fd4 to your computer and use it in GitHub Desktop.
Slick Carousel : Pause YouTube video on slide change
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
// Pause video when slick slide changed | |
$('.act-hero-carousel .the-carousel.carousel-1').on( | |
'beforeChange', | |
function (slick, currentSlide, nextSlide) { | |
var player, command; | |
//find the current slide element and decide which player API we need to use. | |
currentSlide = $('.act-hero-carousel .the-carousel.carousel-1').find( | |
'.slick-current' | |
); | |
//get the iframe inside this slide. | |
player = currentSlide.find('iframe').get(0); | |
console.log({ player }); | |
command = { | |
event: 'command', | |
func: 'pauseVideo', | |
}; | |
//check if the player exists. | |
if (player != undefined) { | |
//post our command to the iframe. | |
player.contentWindow.postMessage(JSON.stringify(command), '*'); | |
} | |
} | |
); | |
// Make sure you add &enablejsapi=1 to the iframe url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment