How to pause a YouTube player when hiding the iframe?(隐藏 iframe 时如何暂停 YouTube 播放器?)
问题描述
我有一个隐藏的 div,其中包含 <iframe>
中的 YouTube 视频.当用户点击链接时,该 div 变为可见,用户应该能够播放视频.
I have a hidden div containing a YouTube video in an <iframe>
. When the user clicks on a link, this div becomes visible, the user should then be able to play the video.
当用户关闭面板时,视频应该停止播放.我怎样才能做到这一点?
When the user closes the panel, the video should stop playback. How can I achieve this?
代码:
<!-- link to open popupVid -->
<p><a href="javascript:;" onClick="document.getElementById('popupVid').style.display='';">Click here</a> to see my presenting showreel, to give you an idea of my style - usually described as authoritative, affable and and engaging.</p>
<!-- popup and contents -->
<div id="popupVid" style="position:absolute;left:0px;top:87px;width:500px;background-color:#D05F27;height:auto;display:none;z-index:200;">
<iframe width="500" height="315" src="aHR0cDovL3d3dy55b3V0dWJlLmNvbS9lbWJlZC9UMzloWUpBd1I0MA==" frameborder="0" allowfullscreen></iframe>
<br /><br />
<a href="javascript:;" onClick="document.getElementById('popupVid').style.display='none';">
close
</a>
</div><!--end of popupVid -->
推荐答案
实现此行为的最简单方法是在必要时调用 pauseVideo
和 playVideo
方法.灵感来自我的 以前的答案,我已经编写了一个无插件函数来实现所需的行为.
The easiest way to implement this behaviour is by calling the pauseVideo
and playVideo
methods, when necessary. Inspired by the result of my previous answer, I have written a pluginless function to achieve the desired behaviour.
唯一的调整:
- 我添加了一个函数,
toggleVideo
- 我已将
?enablejsapi=1
添加到 YouTube 的 URL,以启用该功能
- I have added a function,
toggleVideo
- I have added
?enablejsapi=1
to YouTube's URL, to enable the feature
演示:http://jsfiddle.net/ZcMkt/
代码:
Demo: http://jsfiddle.net/ZcMkt/
Code:
<script>
function toggleVideo(state) {
// if state == 'hide', hide. Else: show video
var div = document.getElementById("popupVid");
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":""}', '*');
}
</script>
<p><a href="javascript:;" onClick="toggleVideo();">Click here</a> to see my presenting showreel, to give you an idea of my style - usually described as authoritative, affable and and engaging.</p>
<!-- popup and contents -->
<div id="popupVid" style="position:absolute;left:0px;top:87px;width:500px;background-color:#D05F27;height:auto;display:none;z-index:200;">
<iframe width="500" height="315" src="aHR0cDovL3d3dy55b3V0dWJlLmNvbS9lbWJlZC9UMzloWUpBd1I0MD9lbmFibGVqc2FwaT0x" frameborder="0" allowfullscreen></iframe>
<br /><br />
<a href="javascript:;" onClick="toggleVideo('hide');">close</a>
这篇关于隐藏 iframe 时如何暂停 YouTube 播放器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:隐藏 iframe 时如何暂停 YouTube 播放器?


- 失败的 Canvas 360 jquery 插件 2022-01-01
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- addEventListener 在 IE 11 中不起作用 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01