我的jQuery UI对话框包含两个div
元素。其中一个具有固定高度,应始终对齐在对话框底部。另一个应占据其余空间。
基本上,我希望所有以蓝色显示的尺寸在调整大小时保持不变。换句话说,红色div
在两个方向调整大小,但绿色div
保持其高度。
在 jQuery UI 或仅使用普通 CSS 中实现这一点的最简单方法是什么?
公司防火墙正在阻止图像,因此我根据其他评论猜测您想要的内容。
编辑: 现在我可以看到您想要什么,我已经相应地更新了我的fiddle。为了完整起见,包括下面的代码。
我会编写一个函数来计算对话框的大小,减去固定div的高度,并将动态div的高度设置为这个计算值。然后,我会将调用此函数的绑定绑定到对话框的resize事件上。这里是一个fiddle,可能需要针对您的确切布局进行一些微调,但应该很接近。
值得注意的一个陷阱是,一些浏览器可能无法正确计算,而它们正在进行滚动/调整大小事件,使用setTimeout排队计算以在调整大小事件完成后解决问题,尽管它使更改在调整大小正在进行时有点跳跃。请参见此问题和答案以获取详细信息。
function SetDivHeight() {
var $DynamicDiv = $('div.dynamic');
var $FixedDiv = $('div.fixed');
var $Container = $(window); //Update for containing element
/*Calculate the adjustment needed to account for the margin and
border of the dynamic div.*/
var HeightAdjustment = $DynamicDiv.outerHeight(true) - $DynamicDiv.height();
/*Get the values of the top and bottom margins which overlap
between the two divs.*/
var DynamicBottomMargin = parseInt($DynamicDiv.css('marginBottom'));
var FixedTopMargin = parseInt($FixedDiv.css('marginTop'));
/*Adjust for the overlapping top/bottom margin by subtracting
which ever is smaller from the adjustment value.*/
if (DynamicBottomMargin >= FixedTopMargin) {
HeightAdjustment -= FixedTopMargin;
} else {
HeightAdjustment -= DynamicBottomMargin;
}
/*subtract the height of the fixed div from the height of the
container, subtract the calculated height adjustment from that
value, and set the result as the height of the dynamic div.*/
$DynamicDiv.height(($Container.height() - $FixedDiv.outerHeight(true)) -
HeightAdjustment);
/*May need to use $Container.innerHeight instead, if container
is not a window element.*/
}
var t;
function QueueSetDivHeight() {
if (t) {
clearTimeout(t);
}
t = setTimeout(function() {
SetDivHeight();
}, 0);
}
$(document).ready(function() {
SetDivHeight();
$(window).resize(QueueSetDivHeight);
//Bind to resize of container element instead/as well
});