问题描述
我正在尝试了解如何使用 YouTube API 定位现有 iframe(即不使用脚本构建 iframe).
像往常一样,Google 没有提供足够的 API 示例,但解释说这是可能的,这里 http://code.google.com/apis/youtube/iframe_api_reference.html
这是我正在尝试做的一个示例 - 缩略图下方的视频应该播放.我快到了,但只有第一个视频播放...
http://jsfiddle.net/SparrwHawk/KtbYR/2/
TL;DR: DEMO: http:///jsfiddle.net/KtbYR/5/
YT_ready
、getFrameID
和 onYouTubePlayerAPIReady
是在 这个答案.这两种方法都可以在没有任何预加载库的情况下实现.在我之前的回答中,我展示了一种实现单帧功能的方法.
在这个答案中,我关注多个帧.
HTML示例代码(重要标签和属性大写,<iframe src id>
):
<img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'><IFRAME ID="frame1" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME></div><img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'><IFRAME ID="frame2" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME></div>JavaScript 代码(YT_ready
、getFrameID
、onYouTubePlayerAPIReady
和 YouTube Frame API 脚本加载器此处)
定义var player = {};//定义一个播放器存储对象,公开方法,//无需再次创建新的类实例.YT_ready(函数() {$(".thumb + iframe[id]").each(function() {var 标识符 = this.id;var frameID = getFrameID(标识符);if (frameID) {//如果框架存在玩家[frameID] = new YT.Player(frameID, {事件:{"onReady": createYTEvent(frameID, 标识符)}});}});});//返回一个启用多个事件的函数函数 createYTEvent(frameID, 标识符) {返回函数(事件){var player = 玩家 [frameID];//设置播放器参考var the_div = $('#'+标识符).parent();the_div.children('.thumb').click(function() {var $this = $(this);$this.fadeOut().next().addClass('play');if ($this.next().hasClass('play')) {播放器.playVideo();}});}}
在我之前的回答中,我绑定了 onStateChange
事件.在此示例中,我使用了 onReady
事件,因为您只想在初始化时调用函数一次.
这个例子的工作原理如下:
this answer中定义了以下方法一个>.
- YouTube Frame API 是从 http://youtube.com/player_api.
- 当这个外部脚本完成加载后,
onYoutubePlayerAPIReady
被调用,这反过来又激活了使用 YT_ready
定义的所有函数
此处显示了以下方法的声明,但使用 这个答案.基于例子的解释:
- 循环遍历每个
<iframe id>
对象,该对象位于 <..class="thumb">
之后. - 在每个框架元素处,检索
id
,并将其存储在 identifier
变量中. - 通过
getFrameID
获取iframe 的内部ID.这可确保为 API 正确格式化 <iframe>
.在这个示例代码中,返回的 ID 等于 identifier
,因为我已经为
附加了一个 ID.李> - 当
<iframe>
存在且有效的 YouTube 视频时,将创建一个新的播放器实例,并将引用存储在 players
对象中,可通过以下方式访问关键 frameID
. - 在创建播放器实例时,会定义一个 **
onReady
* 事件.当框架的 API 完全初始化时,将调用此方法. createYTEvent
此方法返回一个动态创建的函数,该函数为单独的播放器添加了功能.代码中最相关的部分是:
function createYTEvent(frameID, identifier) {返回函数(事件){var player = 玩家 [frameID];//设置播放器参考...播放器.playVideo();}}
frameID
是框架的 ID,用于启用 YouTube Frame API.identifier
是 YT_ready
中定义的 ID,不一定是
元素.getFrameID
将尝试为给定的 id 找到最接近的匹配帧.也就是说,它返回给定 <iframe>
元素的 ID,或者: 如果给定元素不是 <iframe>
,则函数查找子元素这是一个<iframe>
,并返回此帧的ID.如果框架不存在,该函数将通过 -frame
对给定 ID 进行后缀.- players[playerID]` 指的是初始化的播放器实例.
确保您也检查 这个答案,因为这个答案的核心功能就是基于这个.
其他 YouTube Frame API 答案.在这些答案中,我展示了 YouTube Frame/JavaScript API 的各种实现.
I'm trying to understand how to target an existing iframe using the YouTube API (i.e. without constructing an iframe with the script).
As usual, Google does not give enough API examples, but explains that it IS possible, here http://code.google.com/apis/youtube/iframe_api_reference.html
Here is an example of what I'm trying to do - the video underneath the thumbnail should play. I am almost there, but only the first video plays...
http://jsfiddle.net/SparrwHawk/KtbYR/2/
解决方案 TL;DR: DEMO: http://jsfiddle.net/KtbYR/5/
YT_ready
, getFrameID
and onYouTubePlayerAPIReady
are functions as defined in this answer. Both methods can be implemented without any preloaded library. In my previous answer, I showed a method to implement the feature for a single frame.
In this answer, I focus on multiple frames.
HTML example code (important tags and attributes are capitalized, <iframe src id>
):
<div>
<img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
<IFRAME ID="frame1" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>
<div>
<img class='thumb' src='http://i2.cdnds.net/11/34/odd_alan_partridge_bio_cover.jpg'>
<IFRAME ID="frame2" SRC="http://www.youtube.com/embed/u1zgFlCw8Aw?enablejsapi=1" width="640" height="390" frameborder="0"></IFRAME>
</div>
JavaScript code (YT_ready
, getFrameID
, onYouTubePlayerAPIReady
and the YouTube Frame API script loader are defined here)
var players = {}; //Define a player storage object, to expose methods,
// without having to create a new class instance again.
YT_ready(function() {
$(".thumb + iframe[id]").each(function() {
var identifier = this.id;
var frameID = getFrameID(identifier);
if (frameID) { //If the frame exists
players[frameID] = new YT.Player(frameID, {
events: {
"onReady": createYTEvent(frameID, identifier)
}
});
}
});
});
// Returns a function to enable multiple events
function createYTEvent(frameID, identifier) {
return function (event) {
var player = players[frameID]; // Set player reference
var the_div = $('#'+identifier).parent();
the_div.children('.thumb').click(function() {
var $this = $(this);
$this.fadeOut().next().addClass('play');
if ($this.next().hasClass('play')) {
player.playVideo();
}
});
}
}
In my previous answer, I bound the onStateChange
event. In this example, I used the onReady
event, because you want to call the functions only once, at initialization.
This example works as follows:
The following methods are defined in this answer.
- The YouTube Frame API is loaded from http://youtube.com/player_api.
- When this external script has finished loading,
onYoutubePlayerAPIReady
is called, which in his turn activates all functions as defined using YT_ready
The declaration of the following methods are shown here, but implemented using this answer. Explanation based on the example:
- Loops through each
<iframe id>
object, which is placed right after <.. class="thumb">
.
- At each frame element, the
id
is retrieved, and stored in the identifier
variable.
- The internal ID of the iframe is retrieved through
getFrameID
. This ensures that the <iframe>
is properly formatted for the API. In this example code, the returned ID is equal to identifier
, because I have already attached an ID to the <iframe>
.
- When the
<iframe>
exists, and a valid YouTube video, a new player instance is created, and the reference is stored in the players
object, accessible by key frameID
.
- At the creation of the player instance, a **
onReady
* event is defined. This method will be invoked when the API is fully initialized for the frame.
createYTEvent
This method returns a dynamically created function, which adds functionality for separate players. The most relevant parts of the code are:
function createYTEvent(frameID, identifier) {
return function (event) {
var player = players[frameID]; // Set player reference
...
player.playVideo();
}
}
frameID
is the ID of the frame, used to enable the YouTube Frame API.
identifier
is the ID as defined in YT_ready
, not necessarily an <iframe>
element. getFrameID
will attempt to find the closest matching frame for a given id. That is, it returns the ID of a given <iframe>
element, or: If the given element is not an <iframe>
, the function looks for a child which is a <iframe>
, and returns the ID of this frame. If the frame does not exists, the function will postfix the given ID by -frame
.
- players[playerID]` refers to the initialized player instance.
Make sure that you also check this answer, because the core functionality of this answer is based on that.
Other YouTube Frame API answers. In these answers, I showed various implementations of the YouTube Frame/JavaScript API.
这篇关于YouTube API 目标(多个)现有 iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!