我有一个非常奇怪的问题。在每个浏览器和移动版本中,我都会遇到这样的行为:
<代码>F29>
如何避免这个问题?当我第一次听说viewport-height的时候,我很兴奋,我想我可以把它用在固定高度的块上,而不是使用javascript,但现在我认为唯一的方法就是使用javascript的一些resize事件……
你可以在sample site上看到这个问题。
有没有人能帮我/推荐一个CSS解决方案?
简单的测试代码:
/* maybe i can track the issue whe it occours... */
$(function(){
var resized = -1;
$(window).resize(function(){
$('#currenth').val( $('.vhbox').eq(1).height() );
if (++resized) $('#currenth').css('background:#00c');
})
.resize();
})*{ margin:0; padding:0; }
/*
this is the box which should keep constant the height...
min-height to allow content to be taller than viewport if too much text
*/
.vhbox{
min-height:100vh;
position:relative;
}
.vhbox .t{
display:table;
position:relative;
width:100%;
height:100vh;
}
.vhbox .c{
height:100%;
display:table-cell;
vertical-align:middle;
text-align:center;
}<div class="vhbox" style="background-color:#c00">
<div class="t"><div class="c">
this div height should be 100% of viewport and keep this height when scrolling page
<br>
<!-- this input highlight if resize event is fired -->
<input type="text" id="currenth">
</div></div>
</div>
<div class="vhbox" style="background-color:#0c0">
<div class="t"><div class="c">
this div height should be 100% of viewport and keep this height when scrolling page
</div></div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
发布于 2020-03-18 15:55:11
对我来说,这样的把戏造就了一份工作:
height: calc(100vh - calc(100vh - 100%))https://stackoverflow.com/questions/37112218
复制相似问题