我遇到了一个奇怪的(或许不是)行为,我希望避免这种情况,因为最终结果会导致用户体验非常糟糕。为了帮助您理解问题,我编写了下面的代码片段。
var counter;
var counterDisplay;
var intervalRef;
window.onload = function(){
console.log("loading");
counter = 11;
counterDisplay = document.getElementById("spCounter");
intervalRef = setInterval(tickHandler, 1000);
};
function tickHandler(){
counter--;
counterDisplay.innerHTML = counter.toString();
if(counter == 0){
stop();
return;
}
}
function stop(){
clearInterval(intervalRef);
document.getElementById("daddyLongLegs").style.display = "block";
}
html,body{
width:100%;
height:100%;
font-family:Arial;
font-size:16px;
}
.page-wrapper{
height:100%;
background-color:#eee;
}
.growing-element{
height:800px;
display:none;
margin: 100px 100px 0 100px;
background-color:#ddd;
}
<div class="page-wrapper">
<div class="container-element">
<!--This element's height never changes once the page has been rendered-->
<div>
The hidden child element below will magically appear in: <span id="spCounter">10</span> seconds
</div>
<!--This element's height changes anytime after the page has been loaded-->
<div id="daddyLongLegs" class="growing-element">
Now you see me...
</div>
</div>
</div>
#daddyLongLegs
)。为了使“问题”更加明显,将父元素(div.container-element
)颜色区分于子元素。问题:
现在,在10秒后显示子元素(
#daddyLongLegs
)时,它似乎没有“拉伸”父元素(div.container-element
)。这不是我想要实现的行为。我希望父元素在其内容更改时重新调整其高度。但是,重要的是父元素的高度始终覆盖整个文档。问题:
一旦内容更改,如何使父元素重新调整其高度?是否有纯css解决方案?
.page-wrapper
上使用min-height: 100%
代替height: 100%
。 - moped