Skip to content

Instantly share code, notes, and snippets.

@weissmike
Last active March 2, 2021 05:03
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weissmike/6390241 to your computer and use it in GitHub Desktop.
Save weissmike/6390241 to your computer and use it in GitHub Desktop.
Flexslider & YouTube Videos - Pause slider when video plays, pause video when slider advances
jQuery(document).ready(function($) {
$.getScript('//www.youtube.com/iframe_api');
var cnt = 0;
$('.flexslider iframe[src*=youtube]').each(function() {
$(this).attr('id', 'youtubeplayer' + cnt);
cnt++;
});
loadSlider();
function loadSlider() {
if(typeof(YT) != 'undefined') {
$('.flexslider').flexslider({
useCSS: false,
animation: "slide",
directionNav: true,
controlsContainer: ".flex-container",
animationLoop: true,
pauseOnAction: true,
pauseOnHover: true,
video: true,
after: function(slider) {
createPlayers(slider);
},
before: function() {
for (key in players) {
players[key].pauseVideo();
}
},
start: function(slider) {
createPlayers(slider);
},
});
clearTimeout(timeout);
} else {
var timeout = setTimeout(loadSlider, 100);
}
}
var players = {};
function createPlayers(slider) {
var playtimer = [];
(function($) {
$(".flexslider iframe[src*=youtube]").each(function(index) {
var frameID = this.id;
if (frameID) { //If the frame exists
// we check if frame already has associated key in container object
if (!(frameID in players)) {
// now we check if the parent slider "row" is displayed
if ($(this).parents('.flex-active-slide').length > 0) {
// we create the player and add it to the container
players[frameID] = new YT.Player(frameID, {
events: {
"onStateChange": function(event) {
if(event.data == 1 || event.data == 3) {
slider.flexslider('stop');
for (var i=0; i<playtimer.length; i++) {
clearTimeout(playtimer[i]);
}
playtimer = [];
} else {
// API sometimes sends multiple events when seeking
playtimer.push(setTimeout(function(){
slider.flexslider('play');
}, 2000));
}
}
}
});
}
}
}
});
})(jQuery);
}
}
@thedxt
Copy link

thedxt commented Sep 5, 2013

I've been trying to get this to work with flexslider and it just wont work. what does the HTML look like?

@thainguyenit
Copy link

Hi
Can you share source's html? Thank you very much

@thainguyenit
Copy link

Hi all
@thedxt: html for you: http://focode.com/demo/slider/index-video07.html
note: but there is a bug, after The video finishes but The slider don't continues to sliding, please help me to fix this bug.

Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment