加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 资源网站 > 资源 > 正文

html – CSS3 100vh在移动浏览器中不变

发布时间:2020-12-14 21:41:41 所属栏目:资源 来源:网络整理
导读:我有一个非常奇怪的问题…在每个浏览器和手机版本我遇到这种行为: 当您开始滚动页面时,当您加载页面(显示地址栏)时,所有浏览器都有顶部菜单。 100vh仅在视口的可见部分计算,所以当浏览器栏滑动100vh增加(以像素为单位) 所有布局重新绘制并重新调整,因为
我有一个非常奇怪的问题…在每个浏览器和手机版本我遇到这种行为:

>当您开始滚动页面时,当您加载页面(显示地址栏)时,所有浏览器都有顶部菜单。
> 100vh仅在视口的可见部分计算,所以当浏览器栏滑动100vh增加(以像素为单位)
>所有布局重新绘制并重新调整,因为尺寸已更改
>用户体验不好的跳跃效果

怎么可以避免这个问题?当我第一次听到viewport-height我很兴奋,我以为我可以使用它的固定高度块istead使用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 sould 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>

解决方法

不幸的是这是故意的…

这是一个很有知识的问题(至少在safari mobile),这是故意的,因为它可以防止其他问题。 Benjamin Poulain replied to a webkit bug:

This is completely intentional. It took quite a bit of work on our part to achieve this effect.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读