如何在隐藏 iframe 时暂停 YouTube 播放器?
我有一个隐藏的 div,其中包含 中的 YouTube 视频。当用户单击链接时,该 div 变得可见,然后用户应该能够播放视频。
当用户关闭面板时,视频应停止播放。我怎样才能实现这个目标?
代码:
<!-- 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="http://www.youtube.com/embed/T39hYJAwR40" frameborder="0" allowfullscreen></iframe>
<br /><br />
<a href="javascript:;" onClick="document.getElementById('popupVid').style.display='none';">
close
</a>
</div><!--end of popupVid -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(14)
实现此行为的最简单方法是在必要时调用
pauseVideo
和playVideo
方法。受到我的 结果的启发之前的答案,我编写了一个无插件函数来实现所需的行为。唯一的调整:
toggleVideo
?enablejsapi=1
添加到 YouTube 的 URL,以启用该功能演示:http://jsfiddle.net/ZcMkt/
代码:
The easiest way to implement this behaviour is by calling the
pauseVideo
andplayVideo
methods, when necessary. Inspired by the result of my previous answer, I have written a pluginless function to achieve the desired behaviour.The only adjustments:
toggleVideo
?enablejsapi=1
to YouTube's URL, to enable the featureDemo: http://jsfiddle.net/ZcMkt/
Code:
这是 RobW 的 jQuery 回答,用于在模态窗口中隐藏/暂停 iframe:
引用的 html 元素是调用 show / hide 方法的模态 div 本身 (#video-div) 和 iframe (#video-iframe) ),其视频网址为 src="" 并具有后缀 enablejsapi=1?,可实现对播放器的编程控制(例如。
有关 html 的更多信息参见 RobW 的回答。
Here's a jQuery take on RobW's answer for use hiding /pausing an iframe in a modal window:
The html elements referred to are the modal div itself (#video-div) calling the show / hide methods, and the iframe (#video-iframe) which has the video url as is src="" and has the suffix enablejsapi=1? which enables programmatic control of the player (ex. .
For more on the html see RobW's answer.
这是一个简单的 jQuery 片段,用于根据 RobW 和 DrewT 的答案暂停页面上的所有视频:
Here is a simple jQuery snippet to pause all videos on the page based off of RobW's and DrewT's answers:
嘿,一个简单的方法是简单地将视频的 src 设置为空,这样视频在隐藏时就会消失,然后当您单击打开视频的链接时将 src 设置回您想要的视频。只需为 youtube iframe 设置一个 id 并使用该 id 调用 src 函数,如下所示:
Hey an easy way is to simply set the src of the video to nothing, so that the video will desapear while it's hidden an then set the src back to the video you want when you click on the link that opens the video.. to do that simply set an id to the youtube iframe and call the src function using that id like this:
由于您需要在 iframe 的 src 中设置
?enablejsapi=true
才能使用 playVideo /pauseVideo
命令="https://stackoverflow.com/questions/8667882/how-to-pause-a-youtube-player-when-hiding-the-iframe#answer-8668741">其他答案,它通过 Javascript 以编程方式添加此功能可能很有用(特别是如果您希望此行为应用于刚刚剪切并粘贴 YouTube 嵌入代码的其他用户嵌入的视频)。在这种情况下,类似这样的东西可能会很有用:...如果您在
document.ready
上调用此方法,则具有“video”类的 div 中的所有 iframe 都将具有enablejsapi=true
添加到它们的源中,这允许 playVideo /pauseVideo 命令对它们起作用。(注意,本示例使用 jQuery 来设置
var iframes
的那一行,但如果您不使用 jQuery,则一般方法应该与纯 Javascript 一样有效)。Since you need to set
?enablejsapi=true
in the src of the iframe before you can use theplayVideo
/pauseVideo
commands mentioned in other answers, it might be useful to add this programmatically via Javascript (especially if, eg. you want this behaviour to apply to videos embedded by other users who have just cut and paste a YouTube embed code). In that case, something like this might be useful:...if you call this on
document.ready
then all iframes in a div with a class of "video" will haveenablejsapi=true
added to their source, which allows the playVideo / pauseVideo commands to work on them.(nb. this example uses jQuery for that one line that sets
var iframes
, but the general approach should work just as well with pure Javascript if you're not using jQuery).我想分享一个使用 jQuery 提出的解决方案,如果您在单个页面上嵌入了多个 YouTube 视频,该解决方案就可以工作。就我而言,我为每个视频定义了一个模式弹出窗口,如下所示:
在本例中,videoModalXX 代表视频的唯一 ID。然后,以下函数会停止视频:
我喜欢这种方法,因为它可以使视频在您停止的地方暂停,以防您稍后想返回并继续观看。它对我来说效果很好,因为它正在寻找具有特定 id 的视频模式内部的 iframe。不需要特殊的 YouTube 元素 ID。希望有人会发现这也很有用。
I wanted to share a solution I came up with using jQuery that works if you have multiple YouTube videos embedded on a single page. In my case, I have defined a modal popup for each video as follows:
In this case, videoModalXX represents a unique id for the video. Then, the following function stops the video:
I like this approach because it keeps the video paused where you left off in case you want to go back and continue watching later. It works well for me because it's looking for the iframe inside of the video modal with a specific id. No special YouTube element ID is required. Hopefully, someone will find this useful as well.
您可以在隐藏 div 之前通过调用 YouTube 播放器实例上的
stopVideo()
方法来停止视频,例如有关更多详细信息,请参阅此处:http://code.google.com/apis/youtube/js_api_reference.html#Playback_controls
You can stop the video by calling the
stopVideo()
method on the YouTube player instance before hiding the div e.g.For more details see here: http://code.google.com/apis/youtube/js_api_reference.html#Playback_controls
RobW 的方法对我来说非常有效。对于使用 jQuery 的人来说,这是我最终使用的简化版本:
在此示例中,“video_player”是包含 iframe 的父 div。
RobW's way worked great for me. For people using jQuery here's a simplified version that I ended up using:
In this example "video_player" is a parent div containing the iframe.
只需删除 iframe 的 src
just remove src of iframe
Rob W 的回答帮助我弄清楚如何在隐藏滑块时通过 iframe 暂停视频。然而,我需要进行一些修改才能使其正常工作。这是我的 html 片段:
请注意,这适用于 localhost,并且正如 Rob W 提到的,“enablejsapi=1”已添加到视频 URL 的末尾。
以下是我的 JS 文件:
另外,作为一个更简单的示例,请在 JSFiddle 上查看
Rob W answer helped me figure out how to pause a video over iframe when a slider is hidden. Yet, I needed some modifications before I could get it to work. Here is snippet of my html:
Observe that this works on localhosts and also as Rob W mentioned "enablejsapi=1" was added to the end of the video URL.
Following is my JS file:
Also, as a simpler example, check this out on JSFiddle
这种方法需要 jQuery。首先,选择您的 iframe:
现在您选择该 iframe 的按钮播放/暂停并单击它,
我希望它会对您有所帮助。
This approach requires jQuery. First, select your iframe:
Now you select button play/pause of this iframe and click it
I hope it will help you.
RobW 在这里和其他地方的答案非常有帮助,但我发现我的需求要简单得多。我已经在其他地方回答了这个问题,但是也许它在这里也有用。
我有一个方法,可以形成要在 UIWebView 中加载的 HTML 字符串:
您可以忽略准备好的 HTML 字符串中的样式内容。重要的方面是:
当我需要暂停视频时,我只需运行以下命令:
希望有帮助!
RobW's answers here and elsewhere were very helpful, but I found my needs to be much simpler. I've answered this elsewhere, but perhaps it will be useful here also.
I have a method where I form an HTML string to be loaded in a UIWebView:
You can ignore the styling stuff in the preparedHTML string. The important aspects are:
When I need to pause the video, I just run this:
Hope that helps!
效果很好
对于我来说,使用 YT 播放器this.youtube.player.pauseVideo();
This is working fine to me with YT player
this.youtube.player.pauseVideo();
更简洁、优雅、安全的答案:将“?enablejsapi=1”添加到视频 URL 末尾,然后构造并字符串化表示暂停命令的普通对象:
使用
Window.postMessage
方法将生成的 JSON 字符串发送到嵌入的视频文档:请确保为
Window.postMessage
方法的targetOrigin
参数指定视频 URL,以确保您的消息不会被发送给任何非指定收件人。A more concise, elegant, and secure answer: add “?enablejsapi=1” to the end of the video URL, then construct and stringify an ordinary object representing the pause command:
Use the
Window.postMessage
method to send the resulting JSON string to the embedded video document:Make sure you specify the video URL for the
Window.postMessage
method’stargetOrigin
argument to ensure that your messages won’t be sent to any unintended recipient.