问题描述
我正在尝试了解如何使用 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!
The End
相关推荐
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22
前端开发问题
182
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18
前端开发问题
301
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17
前端开发问题
437
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11
前端开发问题
455
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20
前端开发问题
5
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20
前端开发问题
13
热门文章
1错误 [ERR_REQUIRE_ESM]:不支持 ES 模块的 require()
2vue中yarn install报错:info There appears to be trouble with you
3为什么 Chrome(在 Electron 内部)会突然重定向到 chrome-error://chromewebdat
4“aria-hidden 元素不包含可聚焦元素"显示模态时的问题
5使用选择器在 CSS 中选择元素的前一个兄弟
6js报错:Uncaught SyntaxError: Unexpected string
7layui怎么刷新当前页面?
8将模式设置为“no-cors"时使用 fetch 访问 API 时出错
热门精品源码
最新VIP资源
1多功能实用站长工具箱html功能模板
2多风格简历在线生成程序网页模板
3论文相似度查询系统源码
4响应式旅游景点宣传推广页面模板
5在线起名宣传推广网站源码
6酷黑微信小程序网站开发宣传页模板
7房产销售交易中介网站模板
8小学作业自动生成程序


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)