获取要复制的子元素的总高度
Get total height of child elements to duplicate
我有一个固定的位置#header
,其中包含.child
元素的X,高度为19px,隐藏溢出。#header
有一个:hover
,它将高度更改为max-height
(因此它扩展了其中的.child元素)和一个动画transition: max-height 0.2s 1s ease;
。(固定div不能操纵div左右,因为它的增长或缩小),所以我想使position: relative
div低于#header
,具有相同的内容高度#header
的增长或缩小与#header:hover
相同的时间,但可以移动其他div低于它的动画。
我在Stackoverflow上的其他帖子上找到了一种方法,它几乎做到了JSFIDDLE的技巧,除了它将.child元素的所有高度计算在一起。我只需要#parent
的高度,但这是max-height
的高度,而不是固定的高度。
[update] SOLVED thanks to @joel-almeida因为div有一个很短的高度和隐藏溢出,它不能读取div的高度,但添加了。scrollheight后,它可以读取div的高度,无论它是可见的还是隐藏的
当你可以直接得到父高度时,为什么要计算所有子高度?
Try this:
$(function(){
var totalHeight = 0;
totalHeight = $('#parent')[0].scrollHeight;
alert("Total height of all divs: " + totalHeight);
$(".clonechild").height(totalHeight);
});
#parent {
float: left;
width: 500px;
background-color: #e0e0e0;
}
#parent .child {
float:left;
height:100px;
width:100px;
background-color:#666;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
border: 1px solid black;
}
#parent .child , .clonechild p {
font-size: 14px;
color: #fff;
text-align: center;
}
#cloneparent {
position: absolute;
margin: 300px auto auto 0px;
width: 510px;
background-color: #e0e0e0;
}
#cloneparent .clonechild {
float:left;
min-height:100px;
height: ;
width:100px;
background-color:#666;
box-sizing:border-box;
-moz-box-sizing:border-box;
-webkit-box-sizing:border-box;
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="parent">
<div class="child"><p>1</p></div>
<div class="child"><p>2</p></div>
<div class="child"><p>3</p></div>
<div class="child"><p>4</p></div>
<div class="child"><p>5</p></div>
<div class="child"><p>6</p></div>
<div class="child"><p>7</p></div>
<div class="child"><p>8</p></div>
</div>
<p>__#parent clearly not 800px height</p>
<div id="cloneparent">
<div class="clonechild"><p>this needs to be the same height as #parent</p></div>
<p>__#cloneparent clearly not same height as #parent</p>
</div>
相关文章:
- 查找元素高度,包括边距
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- 根据元素高度和宽度的百分比变化增加或减少字体大小
- 更改第二次推送时不起作用的元素高度
- 设置固定元素的最大高度
- 如何根据主体高度动态更改元素边距顶部
- 我需要帮助弄清楚一旦窗口的垂直高度被滚动,如何切换一个元素
- 要更改变量的高度li元素
- 如何在不同的设备模式下强制元素的高度
- 使用JavaScript更改带有窗口高度的元素样式
- 获取不可见元素的高度
- jQuery查找最大元素的高度
- 远距离变换元素的宽度/高度(以像素为单位)
- 使用Javascript匹配两个HTML元素的高度
- 指示中的角度手表元素高度
- 清空并附加元素的扩展高度
- 获取下一个或上一个隐藏元素的高度,以设置父元素的高度.jquery.
- 如何在 React 中使用可变高度元素实现无限滚动
- 如何获取"br"的高度元素使用jquery
- <对象的自动高度/>元素中嵌入的内容