Skip to content

Instantly share code, notes, and snippets.

@aslamdoctor
Created August 15, 2022 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aslamdoctor/8d31a361d89aff3eabe8832cfad59fd4 to your computer and use it in GitHub Desktop.
Save aslamdoctor/8d31a361d89aff3eabe8832cfad59fd4 to your computer and use it in GitHub Desktop.
Slick Carousel : Pause YouTube video on slide change
// 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