2012-01-30 113 views
29

我试图设置一个自动高度包含2个子元素的div,位置固定,并且绝对分段。绝对/固定儿童的父容器上的自动高度

我想我的父容器有一个自动高度,但我知道这是很难的,因为子元素从他们的位置取出页结构。

我已经尝试设置一个高度,以我的父div的工作,但与我的布局是响应,当其缩小到移动时,高度保持不变,因为内容成为堆叠,所以高度需要增加与其子女。

不知道这是有道理的,我没有我的实际代码对我大气压,但香港专业教育学院做了一个小提琴试图解释...当它的孩子

http://jsfiddle.net/dPCky/

回答

29

父DIV不能使用height:auto定位绝对/固定。

您将需要使用JavaScript来实现这一点。

在jQuery的一个例子:

var biggestHeight = 0; 
// Loop through elements children to find & set the biggest height 
$(".container *").each(function(){ 
// If this elements height is bigger than the biggestHeight 
if ($(this).height() > biggestHeight) { 
    // Set the biggestHeight to this Height 
    biggestHeight = $(this).height(); 
} 
}); 

// Set the container height 
$(".container").height(biggestHeight); 

工作实例 http://jsfiddle.net/blowsie/dPCky/1/

+0

Ahh brilliant @Blowise,请问在选择器中*有什么作用?我可以问问这是如何工作的? – Liam 2012-01-30 09:25:52

+1

*选择所有的孩子,我也可以写$(“。container”)。children()。each(),这可能会更有效。 如果你只想选择直接的孩子而不是孩子的孩子,你可以使用$(“。container> *”) – Blowsie 2012-01-30 09:30:57

+2

来阅读*选择器和其他选择器。 http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ – Blowsie 2012-01-30 09:33:42

6

http://jsfiddle.net/dPCky/32/ - 使用float:left;

http://jsfiddle.net/dPCky/40/类似的效果 - 更紧密导致到你想要的效果。

如果你愿意改变你的html,那么你可以做我以上所做的。

我学会从以下教程,我会极力推荐给任何人谁愿意成为HTML/CSS定位职业定位:

http://www.barelyfitz.com/screencast/html-training/css/positioning/

我一般会避免使用JavaScript做的事情时,如果可能的话如果你愿意调整你的html,可能会有一个CSS或HTML级别的修复。

+3

“我通常会避免在可能的情况下使用JavaScript”在这个例子中非常真实,特别是如果你的内容是动态的或者你的宽度是流畅的 – Blowsie 2012-01-30 10:01:17

+0

@Blowsie我可以问为什么你会避免它,如果你的宽度是流畅的?你不能用.resize()运行这个函数吗? – Liam 2012-01-30 12:59:03

+1

@Liam你确实可以使用$(window).resize();事件重新运行你的功能。没有在流体宽度中使用脚本的原因是,如果元素本身具有流体宽度,则元素的高度将根据屏幕窗口宽度而变化,因此每次窗口大小更改时都必须调整它们的大小。 – Blowsie 2012-02-01 10:35:41

-5

一个更简单的方法是:

$(".container").height($(document).height()); 
1

享受集装箱此自动高度,能够适应任何设备和视调整大小或旋转。

它已经通过浮球,内嵌块,绝对,边距和填充进行测试设置为孩子。

<div class="autoheight" style="background: blue"> 
    <div style="position: absolute; width: 33.3%; background: red"> 
    Only 
     <div style="background: green"> 
     First level elements are measured as expected 
     </div> 
    </div> 
    <div style="float:left; width: 33.3%; background: red"> 
    One Two Three Four Five Six Seven Eight Night Ten 
    </div> 
    <div style="float:left; width: 33.3%; background: red"> 
    One Two Three Four Five Six 
    </div> 
</div> 

<script> 
function processAutoheight() 
{ 
    var maxHeight = 0; 

    // This will check first level children ONLY as intended. 
    $(".autoheight > *").each(function(){ 

     height = $(this).outerHeight(true); // outerHeight will add padding and margin to height total 
     if (height > maxHeight) { 
      maxHeight = height; 
     } 
    }); 

    $(".autoheight").height(maxHeight); 
} 

// Recalculate under any condition that the viewport dimension has changed 
$(document).ready(function() { 

    $(window).resize(function() { processAutoheight(); }); 

    // BOTH were required to work on any device "document" and "window". 
    // I don't know if newer jQuery versions fixed this issue for any device. 
    $(document).resize(function() { processAutoheight(); }); 

    // First processing when document is ready 
    processAutoheight(); 
}); 
</script> 
2

有点迟到了,但是这可能帮助别人,因为这是我最近怎么解决的问题,而JS - 如果孩子保持自己的高宽比,因为他们收缩的移动设备。我的例子涉及到使上下文响应jQuery.cycle幻灯片。不确定这是否是你想要达到的上面,但它涉及一个容器中的绝对定位的孩子,它在移动和显示尺寸在大屏幕上具有100%的页面宽度。

您可以将父级的高度设置为使用视口宽度单位(vw),因此高度将根据设备的宽度进行调整。如今支持范围非常广泛,大多数移动设备都将正确使用这些设备,错误和部分支持与vw(而不是vm和vmax在IE中)无关。只有Opera Mini在黑暗中。

在这个例子中(jsfiddle有明确的高度设置),不知道孩子们在做什么,孩子们正在做什么,我们假设孩子的身高相对于设备宽度按比例缩小,这可以让你相当准确地假设基于高宽比的高度。

.container{ height: 75vw; }

http://caniuse.com/#feat=viewport-units

请在caniuse笔记已知问题#7,如果你打算为100vw为宽度的措施,但是在这里我们有高度的玩!

+0

这完全是我需要的......你是我翅膀下的风。 – Dale 2016-01-31 08:01:44

0

相关的@ Blowsie的回答是:

/** 
* Sets the height of a container to its maximum height of its children. This 
* method is required in case 
* 
* @param selector jQuery selector 
*/ 
function setHeightByChildrenHeight(selector) 
{ 
    $(selector).each(function() 
    { 
     var height = 0; 

     $(this).children("*").each(function() 
     { 
      height = Math.max(height, $(this).height()); 
     }); 

     $(this).height(height); 
    }); 
};