Skip to content

Instantly share code, notes, and snippets.

@guillaumepiot
Last active December 5, 2016 09:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guillaumepiot/5585590 to your computer and use it in GitHub Desktop.
Save guillaumepiot/5585590 to your computer and use it in GitHub Desktop.
jQuery - YouTube video play/stop on modal open/hide
$('.close').click(function(){
$('#video').modal('hide');
})
$('#open-video').click(function(){
$('#video').modal({'show':true}).on('hidden', function () {
toggleVideo('hide');
});
toggleVideo();
})
function toggleVideo(state) {
// if state == 'hide', hide. Else: show video
var div = document.getElementById("video-frame");
var iframe = div.getElementsByTagName("iframe")[0].contentWindow;
div.style.display = state == 'hide' ? 'none' : '';
func = state == 'hide' ? 'pauseVideo' : 'playVideo';
iframe.postMessage('{"event":"command","func":"' + func + '","args":""}','*');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment