1
我想创建一个嵌入YouTube视频并允许自定义控件的jQuery Mobile Popup(或任何类型的JavaScript模式)。带自定义YouTube控件的jQuery Mobile弹出窗口
这是一个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 Popup或JavaScript模式中使用自定义YouTube控件?
哇,这是快。谢谢! – kanso
@kanso很高兴帮助。小提琴将完美运作。由于某些原因,该代码段不能在SO内正常运行。 – Iceman