使用 Jquery 阻止 YouTube 视频在 Jquery 滑块中播放

发布于 2024-11-08 11:11:42 字数 1091 浏览 2 评论 0原文

我有一个选项卡式 jquery 滑块,用于在页面上显示整个项目的元素。其中之一恰好是 YouTube 视频,但我没有运气弄清楚当用户单击滑块中的其他内容时如何停止视频。

我查看了其他 stackoverflow 问题,但找不到任何符合我想要的内容,除了这里: 使用 jquery 停止 YouTube 视频?

但是,当使用 vimeo 视频可以更简单地完成此操作时,将视频添加/删除到 dom 以使这项工作似乎过多。 (参见 http://www.taprootfoundation.org/ ...不,我不能只是使用 vimeo :S)

我尝试过这个:

$(document).read(function () {
    $("ul#subNav li a").click(function () {
        $('#video').stopVideo();
    });
});

我还尝试将 $('#video').stopVideo(); 更改为 $(window).find(embed).stopVideo( ); ……或者类似的事情,我脑子里记不起来了。

我也尝试过:

$(document).read(function () {
    $("ul#subNav li a").click(function () {
        var myPlayer = document.getElementById('video');
        myPlayer.stopVideo();
    });
});

“ul#subNav li a”是我控制滑块的链接列表 “#video”是我为嵌入对象指定的 ID。 (我使用的是旧的 Youtube 嵌入,而不是 iframe。)

有什么想法如何专门对 YouTube 视频执行此操作吗?

I have a tabbed jquery slider that I'm using to display elements of an overall project on a page. One of those things happens to be a youtube video, but I'm not having any luck figuring out how to STOP the video when the user clicks through the other content in the slider.

I looked at other stackoverflow questions, but can't find anything that's in line with what I want, except here: Stop a youtube video with jquery?

BUT it seems excessive to add/remove the video to the dom to make this work when it can be done more simply with vimeo videos. (See http://www.taprootfoundation.org/ ... and no, I can't just use vimeo :S)

I've tried this:

$(document).read(function () {
    $("ul#subNav li a").click(function () {
        $('#video').stopVideo();
    });
});

I also tried changing $('#video').stopVideo(); to $(window).find(embed).stopVideo(); ... or something like that, I don't remember it off the to of my head.

And I tried this as well:

$(document).read(function () {
    $("ul#subNav li a").click(function () {
        var myPlayer = document.getElementById('video');
        myPlayer.stopVideo();
    });
});

"ul#subNav li a" is my list of links to control the slider
"#video" is the id I've given the embedded object. (I'm using the old Youtube embed, not the iframes.)

Any ideas how to do this with youtube videos specifically?

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

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

发布评论

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

评论(1

猫卆 2024-11-15 11:11:42

好吧,在浏览 Google API 文档(Google 拥有 Youtube)后,我经历了一些严重的头痛和困惑,我从他们的示例中拼凑出代码来使一些东西发挥作用。这确实不理想,因为当我尝试编写自己的等价物时,我并不 100% 理解发生了什么以及为什么事情不起作用。

我的滑块设置为使用 div 作为每张幻灯片的“容器”。整个代码进入我想要视频的div。

<!--this is only displayed when the browser does not have flash or js -->
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.
</div>

<script type="text/javascript">
//calls the video player by ID so it can be accessed later
function onYouTubePlayerReady(playerId){ytplayer = document.getElementById("playerID");}
     //playerID is arbitrary, what you would normally give to your object or embed element
     var params = { allowScriptAccess: "always" };var atts = { id: "playerID" };

     //this next line totally replaces the object/embed element normally used.
     //be careful when replacing the URL below, only replace the URL up to the question mark (the end of the video ID), the bits after that are REQUIRED for this to work.
     //425/356 etc are sizes of the video
     swfobject.embedSWF("http://www.youtube.com/e/videoID?enablejsapi=1&version=3&playerapiid=ytplayer","ytapiplayer", "425", "356", "8", null, null, params, atts);

     //function to have the video play
     function play() {if (ytplayer) {ytplayer.playVideo();}}

     //function to have the video pause -- apparently using stopVideo isn't best practice. Check GoogleApi for more info: http://code.google.com/apis/youtube/js_api_reference.html#Playback_controls
     function pause() {if (ytplayer) {ytplayer.pauseVideo();}}
</script>

这是我的链接代码,在一个完全不同的div中找到:

<a href="#one" rel="1" onclick="pause();"> One</a>
<a href="#two" rel="1" onclick="pause();"> Two</a>
<a href="#three" rel="1" onclick="play();"> Three</a>

我考虑让包含以下内容的div自动播放视频(通过在链接中使用“play();”)视频,但我想我实际上不会这样做,自动播放视频在其他人的网站上让我发疯,所以我不想让其他人在我自己的网站上受到这种影响。

这样你就得到了。一些有效的代码。虽然我完全不知道为什么。希望其他人觉得这有帮助!

**不要忘记确保您的页面链接了 swfobject.js 文件!
我在文档的头部做了这个:

<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script><!--Uses Google API library-->

**更新:
不适用于 Safari 或 Firefox,但适用于 IE8 和 Chrome。我很想知道为什么,因为通常 Firefox 或 IE 本身运行不佳,而 Safari 和 Chrome 一样都是基于 webkit 的。

您可能有的任何想法都会有所帮助。在那之前,我将寻找新的解决方案,并在找到它时发布它。

**更新 #2:实际上可以在所有四种主要浏览器(Chrome、Safari、Firefox 3.6 和 IE8 - 未针对低于 IE8 或 FF3.6 的版本进行测试)中运行,结果我需要更新 Safari 和 FF 的插件支持 Flash,因此非 Flash 消息实际上在应该显示的时候显示。请想象我捂脸的样子。

耶,工作解决方案!

Okay, after some serious headaches and confusion looking through the Google API docs (Google owns Youtube) I pieced together code from their examples to make something work. It's really not ideal because I don't 100% understand what's going on and why things weren't working when I was trying to write my own equivalent.

My slider is set up to use divs as the "container" for each slide. This entire code goes into the div I want the video in.

<!--this is only displayed when the browser does not have flash or js -->
<div id="ytapiplayer">You need Flash player 8+ and JavaScript enabled to view this video.
</div>

<script type="text/javascript">
//calls the video player by ID so it can be accessed later
function onYouTubePlayerReady(playerId){ytplayer = document.getElementById("playerID");}
     //playerID is arbitrary, what you would normally give to your object or embed element
     var params = { allowScriptAccess: "always" };var atts = { id: "playerID" };

     //this next line totally replaces the object/embed element normally used.
     //be careful when replacing the URL below, only replace the URL up to the question mark (the end of the video ID), the bits after that are REQUIRED for this to work.
     //425/356 etc are sizes of the video
     swfobject.embedSWF("http://www.youtube.com/e/videoID?enablejsapi=1&version=3&playerapiid=ytplayer","ytapiplayer", "425", "356", "8", null, null, params, atts);

     //function to have the video play
     function play() {if (ytplayer) {ytplayer.playVideo();}}

     //function to have the video pause -- apparently using stopVideo isn't best practice. Check GoogleApi for more info: http://code.google.com/apis/youtube/js_api_reference.html#Playback_controls
     function pause() {if (ytplayer) {ytplayer.pauseVideo();}}
</script>

This is my link code, found in a totally different div:

<a href="#one" rel="1" onclick="pause();"> One</a>
<a href="#two" rel="1" onclick="pause();"> Two</a>
<a href="#three" rel="1" onclick="play();"> Three</a>

I considered having the video autoplay (by using "play();" in the link) for the div containing the video but I think I actually won't do that, autoplay videos drive me crazy on other peoples' sites so I don't want to subject other people to that on my own site.

So there you have it. Some code that works. Although I don't totally know why. Hope someone else finds this helpful!

**Don't forget to make sure you've got a swfobject.js file linked to your page!
I did this in the head of my document:

<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js" type="text/javascript"></script><!--Uses Google API library-->

**UPDATED:
Does not work in Safari or Firefox, but does work in IE8 and Chrome. Curious to know why because it's usually Firefox or IE that don't play nice on their own, and Safari is webkit based like Chrome.

Any ideas you might have would be helpful. Until then I will work for a new solution and post it when/if I find it.

**Update #2: Does actually work in all four major browsers (Chrome, Safari, Firefox 3.6 & IE8 -- not tested for less than IE8 or FF3.6) Turns out I needed to update plugins for both Safari and FF to support Flash so the non-flash message was in fact displaying when it should have. Please imagine me face-palming.

Yay for working solutions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文