Javascript 如何使用 jQuery 停止 Vimeo 视频
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6145990/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to stop a Vimeo video with jQuery
提问by Justin Meltzer
When I hide a YouTube video, it stops playing. However, this is not the case for Vimeo videos. Is there another way to stop a Vimeo video?
当我隐藏 YouTube 视频时,它会停止播放。但是,Vimeo 视频并非如此。还有其他方法可以停止 Vimeo 视频吗?
回答by Deborah
First, add an ID to your iFrame. Then add this to your javascript close window click function:
首先,为您的 iFrame 添加一个 ID。然后将此添加到您的 javascript 关闭窗口单击功能中:
var $frame = $('iframe#yourIframeId');
// saves the current iframe source
var vidsrc = $frame.attr('src');
// sets the source to nothing, stopping the video
$frame.attr('src','');
// sets it back to the correct link so that it reloads immediately on the next window open
$frame.attr('src', vidsrc);
回答by Michelle Tilley
Vimeo has a JavaScript APIthat allows you to access and invoke many properties and methods on the video player (including pausing the video and also unloading it completely). They also have an API Playgroundand some examples on GitHub.
Vimeo 有一个 JavaScript API,允许您访问和调用视频播放器上的许多属性和方法(包括暂停视频和完全卸载视频)。他们在 GitHub 上还有一个API Playground和一些示例。
[Edit]
[编辑]
Since you mention that you use the Universal Embed Code, here are some caveats from the web site:
由于您提到您使用通用嵌入代码,以下是网站上的一些警告:
With the Universal Embed Code, the only way to interact with the player is by using window.postMessage. postMessage is a relatively new development, so it's oly available in the following browsers: Internet Explorer 8+, Firefox 3+, Safari 4+, Chrome, and Opera 9+.
Because of the complexities involved with postMessage, we've written a JS mini-library that does all the hard work for you! You can find it on the downloads pageor you can see some examples below.
使用通用嵌入代码,与播放器交互的唯一方法是使用window.postMessage。postMessage 是一个相对较新的开发,因此它只能在以下浏览器中使用:Internet Explorer 8+、Firefox 3+、Safari 4+、Chrome 和 Opera 9+。
由于 postMessage 所涉及的复杂性,我们编写了一个 JS 迷你库来为您完成所有繁重的工作!您可以在下载页面上找到它,或者您可以在下面看到一些示例。
回答by johna
I recently needed to pause a Vimeo video that was inside a Bootstrap modal when the modal was closed.
我最近需要在模式关闭时暂停位于 Bootstrap 模式内的 Vimeo 视频。
The Vimeo video was embedded in an iframe.
Vimeo 视频嵌入在 iframe 中。
This is what worked for me:
这对我有用:
$("#my-bootstrap-modal").on('hidden.bs.modal', function (e) {
var div = document.getElementById("my-bootstrap-modal");
var iframe = div.getElementsByTagName("iframe")[0].contentWindow;
iframe.postMessage('{"method":"pause"}', '*');
});
回答by pirios
To restore the SRC attribute, use the following before clearing:
要恢复 SRC 属性,请在清除前使用以下命令:
var source = $('iframe#yourVideoId').attr('src');
Next, SRC attribute clear:
接下来,清除src属性:
$('iframe#yourVideoId').attr('src', '');
Callback previous SRC attribute:
回调之前的 SRC 属性:
$('iframe#yourVideoId').attr('src', source);
回答by user2194405
var vidUrl = $("iframe#video-frame").attr('src');
//Basically stops and starts the video on modal open/close
$('#video').on('hidden.bs.modal', function (e) {
$("iframe#video-frame").attr('src','');
});
$('#video').on('show.bs.modal', function (e) {
$("iframe#video-frame").attr('src', vidUrl);
})
回答by Nathan J
Another answer along the lines of David's...you can use jQuery to clear the SRC attribute of the iFrame.
大卫的另一个答案……您可以使用 jQuery 清除 iFrame 的 SRC 属性。
$('iframe#targetID').attr('src','');
I'm using this with a Vimeo video and a lightbox effect. When the lightbox is triggered again, I add the video URL back to the iFrame SRC before showing it.
我将它与 Vimeo 视频和灯箱效果一起使用。当灯箱再次被触发时,我会在显示之前将视频 URL 添加回 iFrame SRC。
回答by trushkevich
Using the Vimeo JavaScript API, it can be done with:
使用 Vimeo JavaScript API,可以通过以下方式完成:
player.unload()