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

前端之家收集整理的这篇文章主要介绍了html – CSS3 100vh在移动浏览器中不变前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个非常奇怪的问题…在每个浏览器和手机版本我遇到这种行为:

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

怎么可以避免这个问题?当我第一次听到viewport-height我很兴奋,我以为我可以使用它的固定高度块istead使用javascript,但现在我认为唯一的办法是事实上javascript与一些resize事件…

你可以看到这个问题:sample site

任何人可以帮助我/提出一个CSS解决方案?

简单测试代码

  1. /* maybe i can track the issue whe it occours... */
  2. $(function(){
  3. var resized = -1;
  4. $(window).resize(function(){
  5. $('#currenth').val( $('.vhBox').eq(1).height() );
  6. if (++resized) $('#currenth').css('background:#00c');
  7. })
  8. .resize();
  9. })
  1. *{ margin:0; padding:0; }
  2.  
  3. /*
  4. this is the Box which sould keep constant the height...
  5. min-height to allow content to be taller than viewport if too much text
  6. */
  7. .vhBox{
  8. min-height:100vh;
  9. position:relative;
  10. }
  11.  
  12. .vhBox .t{
  13. display:table;
  14. position:relative;
  15. width:100%;
  16. height:100vh;
  17. }
  18.  
  19. .vhBox .c{
  20. height:100%;
  21. display:table-cell;
  22. vertical-align:middle;
  23. text-align:center;
  24. }
  1. <div class="vhBox" style="background-color:#c00">
  2. <div class="t"><div class="c">
  3. this div height should be 100% of viewport and keep this height when scrolling page
  4. <br>
  5. <!-- this input highlight if resize event is fired -->
  6. <input type="text" id="currenth">
  7. </div></div>
  8. </div>
  9.  
  10. <div class="vhBox" style="background-color:#0c0">
  11. <div class="t"><div class="c">
  12. this div height should be 100% of viewport and keep this height when scrolling page
  13. </div></div>
  14. </div>
  15.  
  16. <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.

猜你在找的HTML相关文章