首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CSS3 100vh在移动浏览器中不是常量

CSS3 100vh在移动浏览器中不是常量
EN

Stack Overflow用户
提问于 2016-05-09 17:32:40
回答 29查看 286K关注 0票数 428

我有一个非常奇怪的问题。在每个浏览器和移动版本中,我都会遇到这样的行为:

  • 当您加载页面(例如显示地址栏)时,所有浏览器都有一个顶部菜单,当您开始滚动页面时,该菜单会向上滑动。
  • 100vh有时仅在视口的可见部分计算,因此当浏览器栏向上滑动100vh时,
  • 所有布局都会增加(以像素为单位),并重新绘制和调整所有布局,因为尺寸发生了变化
  • 对用户体验产生了不好的跳跃效果

<代码>F29>

如何避免这个问题?当我第一次听说viewport-height的时候,我很兴奋,我想我可以把它用在固定高度的块上,而不是使用javascript,但现在我认为唯一的方法就是使用javascript的一些resize事件……

你可以在sample site上看到这个问题。

有没有人能帮我/推荐一个CSS解决方案?

简单的测试代码:

代码语言:javascript
运行
复制
/* 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();
})
代码语言:javascript
运行
复制
*{ 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;
}
代码语言:javascript
运行
复制
<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>

EN

Stack Overflow用户

发布于 2020-03-18 15:55:11

对我来说,这样的把戏造就了一份工作:

代码语言:javascript
运行
复制
height: calc(100vh - calc(100vh - 100%))
票数 47
EN
查看全部 29 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37112218

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档