【发布时间】:2016-12-24 06:47:57
【问题描述】:
我想创建一个嵌入 YouTube 视频并允许自定义控件的 jQuery Mobile 弹出窗口(或任何类型的 javascript 模式)。
这是一个JSFiddle demo,带有一个基本的“播放”按钮,希望能帮助说明我的问题。
基本上,我有 HTML 中的 iframe 代码:
<iframe id="iframe-video" width="400" height="300" src="http://www.youtube.com/embed/a0gact_tf_0" frameborder="0" allowfullscreen></iframe>
然后,当加载视频时,我会这样做:
$(document).on("pagecreate", function() {
// bind click events
$(document).on('click', '#play', function() {
playVideo();
return false;
});
$(".ui-popup iframe")
.attr("width", 0)
.attr("height", "auto");
$("#popupVideo").on({
popupbeforeposition: function() {
initYoutubePlayer();
// call our custom function scale() to get the width and height
var size = scale(497, 298, 15, 1),
w = size.width,
h = size.height;
$("#popupVideo iframe")
.attr("width", w)
.attr("height", h);
},
popupafterclose: function() {
$("#popupVideo").html("<span></span>");
$("#popupVideo iframe")
.attr("width", 0)
.attr("height", 0);
},
});
});
function initYoutubePlayer() {
var tag = document.createElement('script');
tag.src = 'https://www.youtube.com/iframe_api';
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
alert('onYouTubeIframeAPIReady');
player = new YT.Player('iframe-video', {
events: {
'onReady': onPlayerReady,
}
});
};
function onPlayerReady(event) {
alert('ready');
};
alert('initialized');
}
function playVideo() {
alert('playVideo...');
//player.playVideo();
}
加载视频工作正常,但我无法让 YouTube iFrame API 在我可以实例化 YT.Player 的地方工作。换句话说,“onYouTubeIframeAPIReady”不会触发。
我已经尝试了这个 JSFiddle 代码的各种排列,但感觉我可能遗漏了一些明显的东西。
是否可以在 jQuery Mobile 弹出窗口或 javascript 模态中拥有自定义 YouTube 控件?
【问题讨论】:
标签: javascript jquery jquery-mobile youtube-api youtube-iframe-api