简体   繁体   English

div组的动态div高度

[英]Dynamic div height for set of divs

I have a parent div and two children. 我有一个父母div和两个孩子。 I want the height of the children to remain constant at 75% and 25% of the parent height. 我希望孩子的身高保持在父母身高的75%和25%不变。

The issue I have is that I need the parent to have a height set by a background image. 我遇到的问题是我需要父级通过背景图像设置高度。 That is it should shrink responsively but always present the same area of the image. 也就是说,它应该相应地缩小,但总是呈现图像的相同区域。

Here is aa diagram of the behaviour: 这是一个行为图:

在此输入图像描述 Child A contains text. 儿童A包含文字。 To be totally honest I don't care how high child A is as long as the text is displayed. 说实话,只要文本显示,我就不在乎孩子A的高度。 I need child B to maintain it's height as a proportion of the parent and its position at the bottom of the parent div. 我需要孩子B来保持它的高度作为父母的比例及其在父母div底部的位置。

I would like to know if there is a reasonable pure css solution to this problem. 我想知道是否有一个合理的纯css解决方案来解决这个问题。

This issue is addressed here 这个问题在这里解决

div {
    background-image: url('http://www.pets4homes.co.uk/images/articles/1111/large/feline-influenza-all-about-cat-flu-5239fffd61ddf.jpg');
    background-size: contain;
    background-repeat: no-repeat;
    width: 100%;
    height: 0;
    padding-top: 66.64%; /* (img-height / img-width * container-width) */
                /* (853 / 1280 * 100) */

    position:relative;
}

Add these styles to child "div" 将这些样式添加到子“div”

A div=> 一个div =>

width:100%;height:75%;background-color: lightblue;position:absolute; top:0; bottom:0; left:0

B div=> B div =>

width:100%;height:25%;background-color: green;position:absolute; top:75%; bottom:0; left:0; right:0;

here i have a jquery solution. 在这里,我有一个jquery解决方案。

var h=$('#parentId').innerHeight;
$('#childA').css('height',h*(75/100)+'px');
$('#childB').css('height',h*(25/100)+'px');

using innerHeight method u can get height of any element . 使用innerHeight方法你可以获得任何元素的高度。

I think you have a number of options: 我想你有很多选择:

If you are using AngularJS, you can set the height and width of the inner divs as $scope variables. 如果您使用的是AngularJS,则可以将内部div的高度和宽度设置为$ scope变量。 Then, when the parent div changes size (you can trigger an event on it) you update the value of these variables. 然后,当父div更改大小(您可以在其上触发事件)时,您更新这些变量的值。 For instance: 例如:

<div style="height:{{Inner_Div_Top_Height}}>
   ...
</div>

and in your controller: 并在您的控制器中:

$scope.Inner_Div_Top_Height = <calculated height based on parent div's height> ;

Same for the lower div. 同样适用于较低的div。

If, on the other hand, you are not using Angular, you will need to manually alter the style of the elements (inner divs). 另一方面,如果您没有使用Angular,则需要手动更改元素的样式(内部div)。 For instance: 例如:

document.getElementById("Inner_Upper_Div").style.height = <calculated height based on parent div's height> ;

Hope I understood your question correctly. 希望我能正确理解你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM