Is it possible to pause a YouTube video in an iframe using JavaScript?

喜欢而已 提交于 2020-01-06 02:21:27

问题


I have a slider that I'm making which displays an image for my post, then when you click it, it makes the image be replaced by an iframe with a YouTube embed. I want to be able to somehow pause the videos using JavScript (or jQuery) by utilizing the iframes ID. Is there a simple way to do this?


回答1:


You can pause a youtube video.

You can try looking at this code here:

http://jsfiddle.net/3J2wT/757/

HTML:

<a id="pause" href="#">Pause</a>

<iframe id="popup-youtube-player" width="640" height="360" src="http://www.youtube.com/embed/geTgZcHrXTc?enablejsapi=1&version=3&playerapiid=ytplayer" frameborder="0" allowfullscreen="true" allowscriptaccess="always">
</iframe>

Javascript:

$('#pause').on('click', function() {

    $('#popup-youtube-player')[0].contentWindow.postMessage('{"event":"command","func":"' + 'pauseVideo' + '","args":""}', '*');    

});



来源:https://stackoverflow.com/questions/23295860/is-it-possible-to-pause-a-youtube-video-in-an-iframe-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!