Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.