问题是:
- iframe 内容来自另一个域
- 需要针对每个视频调整 iframe 尺寸
解决方案基于奥莫柯克的回答 https://stackoverflow.com/a/5660409/87015涉及:
- 创建 iframe 元素
- 创建一个对话框
autoOpen: false, width: "auto", height: "auto"
- 指定 iframe 源、宽度和高度before打开对话框
这是代码的粗略轮廓:
HTML
<div class="thumb">
<a href="http://jsfiddle.net/yBNVr/show/" data-title="Std 4:3 ratio video" data-width="512" data-height="384"><img src="http://dummyimage.com/120x90/000/f00&text=Std+4-3+ratio+video" /></a></li>
<a href="http://jsfiddle.net/yBNVr/1/show/" data-title="HD 16:9 ratio video" data-width="512" data-height="288"><img src="http://dummyimage.com/120x90/000/f00&text=HD+16-9+ratio+video" /></a></li>
</div>
jQuery
$(function () {
var iframe = $('<iframe frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe>');
var dialog = $("<div></div>").append(iframe).appendTo("body").dialog({
autoOpen: false,
modal: true,
resizable: false,
width: "auto",
height: "auto",
close: function () {
iframe.attr("src", "");
}
});
$(".thumb a").on("click", function (e) {
e.preventDefault();
var src = $(this).attr("href");
var title = $(this).attr("data-title");
var width = $(this).attr("data-width");
var height = $(this).attr("data-height");
iframe.attr({
width: +width,
height: +height,
src: src
});
dialog.dialog("option", "title", title).dialog("open");
});
});
演示在这里 http://jsfiddle.net/H7uar/1/show/ and 代码在这里 http://jsfiddle.net/H7uar/1/. And 另一个类似的例子 http://salman-w.blogspot.com/2013/05/jquery-ui-dialog-examples.html#example-6