Last active
December 5, 2016 09:25
-
-
Save guillaumepiot/5585590 to your computer and use it in GitHub Desktop.
jQuery - YouTube video play/stop on modal open/hide
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
$('.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