随着时间的推移,我确实改进了@TrueBlueAussie 的答案,并认为我会在此处发布更复杂的答案以供将来参考。
在这里将其作为 GitHub 上的插件:https ://github.com/drewbaker/fitToParent
这是jQuery插件:
jQuery.fn.fitToParent = function (options) {
this.each(function () {
// Cache the resize element
var $el = jQuery(this);
// Get size parent (box to fit element in)
var $box;
if( $el.closest('.size-parent').length ) {
$box = $el.closest('.size-parent');
} else {
$box = $el.parent();
}
// These are the defaults.
var settings = jQuery.extend({
height_offset: 0,
width_offset: 0,
box_height: $box.height(),
box_width: $box.width(),
}, options );
// Setup box and element widths
var width = $el.width();
var height = $el.height();
var parentWidth = settings.box_width - settings.width_offset;
var parentHeight = settings.box_height - settings.height_offset;
// Maintin aspect ratio
var aspect = width / height;
var parentAspect = parentWidth / parentHeight;
// Resize to fit box
if (aspect > parentAspect) {
newWidth = parentWidth;
newHeight = (newWidth / aspect);
} else {
newHeight = parentHeight;
newWidth = newHeight * aspect;
}
// Set new size of element
$el.width(newWidth);
$el.height(newHeight);
});
};
所以,假设你有这样的 HTML:
<div id="wrapper">
<iframe width="720" height="405" src="//player.vimeo.com/video/19223989"></iframe>
</div>
调用插件最基本的方式是这样的:
jQuery('#wrapper iframe').fitToParent();
但我经常将#wrapper 设置为接近窗口的高度和宽度,如下所示:
// Get window height and width
var winHeight = jQuery(window).height();
var winWidth = jQuery(window).width();
// Set wrapper height/width to that of window
jQuery('#wrapper').height(winHeight).width(winWidth);
// Size Iframe
jQuery('#wrapper iframe').fitToParent({
height_offset: 100, // Put some space around the video
width_offset: 100, // Put some space around the video
});
您还可以输入自定义框大小以适应元素,如下所示:
// Get window height and width
var winHeight = jQuery(window).height();
var winWidth = jQuery(window).width();
// Size element
jQuery('#wrapper iframe').fitToParent({
height_offset: 100, // Put some space around the video
width_offset: 100, // Put some space around the video
box_height: winHeight, // Force use of this box size
box_width: winWidth // Force use of this box size
});
我还添加了将 CSS 类“size-parent”设置为父元素的功能,然后它将使用该父元素作为框大小。一个完整的例子:
// HTML like this
<div id="wrapper" class="size-parent">
<div class="media">
<iframe width="720" height="405" src="//player.vimeo.com/video/19223989"></iframe>
</div>
</div>
// jQuery like this
jQuery('.media iframe').fitToParent();
如果您不设置 .size-parent,它将回退到元素父级。如果您设置 box_height/box_width 参数,那么这些参数显然会覆盖所有内容。
现在,为了展示它的强大功能,试试这个垂直居中、水平居中宽高比正确的 iFrame!
// CSS like this
#wrapper {
text-align: center;
display: table-cell;
vertical-align: middle;
}
#wrapper iframe {
display: inline-block;
}
// HTML like this
<div id="wrapper" class="size-wrapper">
<iframe width="720" height="405" src="//player.vimeo.com/video/19223989"></iframe>
</div>
// jQuery like this
// Get window height and width
var winHeight = jQuery(window).height();
var winWidth = jQuery(window).width();
// Size wrapper
jQuery('#wrapper').height( winHeight ).width( winWidth );
// Size element
jQuery('#wrapper iframe').fitToParent({
height_offset: 200, // Put some space around the video
width_offset: 200, // Put some space around the video
});
// Fit iFrame to wrapper
jQuery('#wrapper iframe').fitToParent();
在现实生活中,我将 jQuery 包装在一个函数中,然后在调整窗口大小时调用该函数以获得真正的响应式 iFrame!