如何使用 jQuery 停止 Vimeo 视频

发布于 2024-11-10 03:35:44 字数 71 浏览 3 评论 0原文

当我隐藏 YouTube 视频时,它会停止播放。然而,Vimeo 视频的情况并非如此。还有其他方法可以停止 Vimeo 视频吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

怀念你的温柔 2024-11-17 03:35:44

首先,将 ID 添加到您的 iFrame。然后将其添加到您的 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);

First, add an ID to your iFrame. Then add this to your javascript close window click function:

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);
香草可樂 2024-11-17 03:35:44

最近,当模式关闭时,我需要暂停 Bootstrap 模式内的 Vimeo 视频。

Vimeo 视频嵌入到 iframe 中。

这对我有用:

$("#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"}', '*');
});

I recently needed to pause a Vimeo video that was inside a Bootstrap modal when the modal was closed.

The Vimeo video was embedded in an 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"}', '*');
});
白龙吟 2024-11-17 03:35:44

Vimeo 有JavaScript API,允许您访问和调用视频上的许多属性和方法播放器(包括暂停视频并完全卸载)。他们还有一个 API Playground 和一些 GitHub 上的示例

[编辑]

既然您提到您使用通用嵌入代码,那么以下是该网站的一些注意事项:

使用通用嵌入代码,与播放器交互的唯一方法是使用 window .postMessage。 postMessage 是一个相对较新的开发,因此仅在以下浏览器中可用:Internet Explorer 8+、Firefox 3+、Safari 4+、Chrome 和 Opera 9+。

由于 postMessage 涉及的复杂性,我们编写了一个 JS 迷你库来为您完成所有艰苦的工作!您可以在下载页面上找到它,也可以查看一些示例下面

Vimeo has a JavaScript API that 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 Playground and some examples on GitHub.

[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 page or you can see some examples below.

朱染 2024-11-17 03:35:44

要恢复 SRC 属性,请在清除之前使用以下命令:

var source = $('iframe#yourVideoId').attr('src');

接下来,SRC 属性清除:

$('iframe#yourVideoId').attr('src', '');

回调之前的 SRC 属性:

$('iframe#yourVideoId').attr('src', source);

To restore the SRC attribute, use the following before clearing:

var source = $('iframe#yourVideoId').attr('src');

Next, SRC attribute clear:

$('iframe#yourVideoId').attr('src', '');

Callback previous SRC attribute:

$('iframe#yourVideoId').attr('src', source);
┈┾☆殇 2024-11-17 03:35:44
    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);
    })
    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);
    })
眼眸里的那抹悲凉 2024-11-17 03:35:44

与 David 类似的另一个答案...您可以使用 jQuery 清除 iFrame 的 SRC 属性。

$('iframe#targetID').attr('src','');

我将其与 Vimeo 视频和灯箱效果一起使用。当灯箱再次触发时,我将视频 URL 添加回 iFrame SRC,然后再显示。

Another answer along the lines of David's...you can use jQuery to clear the SRC attribute of the iFrame.

$('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.

一场春暖 2024-11-17 03:35:44

使用 Vimeo JavaScript API,可以通过以下方式完成:

player.unload()

https://github。 com/vimeo/player.js#unload-promisevoid-error

Using the Vimeo JavaScript API, it can be done with:

player.unload()

https://github.com/vimeo/player.js#unload-promisevoid-error

把人绕傻吧 2024-11-17 03:35:44

您必须删除并重新安装 src。

    $('#helpVideo').on('hidden.bs.modal', function () {
        video_src = $('iframe').attr('src')
        $('iframe').attr('src',video_src)
    })

You have to remove and put the src back on.

    $('#helpVideo').on('hidden.bs.modal', function () {
        video_src = $('iframe').attr('src')
        $('iframe').attr('src',video_src)
    })
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文