【问题标题】:How to prevent logo from causing navigation bar from jittering while it's loading如何防止徽标在加载时导致导航栏抖动
【发布时间】:2017-06-20 15:19:21
【问题描述】:

我一直在尝试寻找解决方案一段时间,但似乎没有任何解决方案。

导航到网站上的任何和所有页面时都会出现这个问题 - 这非常烦人。

虽然我希望加载网站图片需要一些时间,但这种加载会影响我的导航栏和网站徽标的加载。在加载每个页面的时间里,我的网站的标志完全不存在——这导致我的导航栏一直向上移动,直到出现标志。这通常需要大约一瞬间,但也完全取决于用户的互联网连接)。

如何防止这种情况发生?这会导致我的整个网站在导航时“弹跳”,所有内容都会在没有徽标的情况下短暂上移。

【问题讨论】:

  • 1- 使用预加载器 / 2- 延迟加载图像 / 3- 确保图像是渐进式 JPEG(我建议结合使用 2 和 3)

标签: html css navbar loading image-loading


【解决方案1】:

给你的图片标签一个绝对高度属性。这将使浏览器将 img 标签保持在应有的高度,并允许元素加载到正确的位置。

【讨论】:

    【解决方案2】:

    您也可以尝试调整加载器,使其仅在页面中的所有元素都加载后才加载页面。就这么简单:

    <!DOCTYPE html>
    <html>
    <head>
    <style>
    /* Center the loader */
    #loader {
      position: absolute;
      left: 50%;
      top: 50%;
      z-index: 1;
      width: 150px;
      height: 150px;
      margin: -75px 0 0 -75px;
      border: 16px solid #f3f3f3;
      border-radius: 50%;
      border-top: 16px solid #3498db;
      width: 120px;
      height: 120px;
      -webkit-animation: spin 2s linear infinite;
      animation: spin 2s linear infinite;
    }
    
    @-webkit-keyframes spin {
      0% { -webkit-transform: rotate(0deg); }
      100% { -webkit-transform: rotate(360deg); }
    }
    
    @keyframes spin {
      0% { transform: rotate(0deg); }
      100% { transform: rotate(360deg); }
    }
    
    /* Add animation to "page content" */
    .animate-bottom {
      position: relative;
      -webkit-animation-name: animatebottom;
      -webkit-animation-duration: 1s;
      animation-name: animatebottom;
      animation-duration: 1s
    }
    
    @-webkit-keyframes animatebottom {
      from { bottom:-100px; opacity:0 } 
      to { bottom:0px; opacity:1 }
    }
    
    @keyframes animatebottom { 
      from{ bottom:-100px; opacity:0 } 
      to{ bottom:0; opacity:1 }
    }
    
    #myDiv {
      display: none;
      text-align: center;
    }
    </style>
    </head>
    <body onload="myFunction()" style="margin:0;">
    
    <div id="loader"></div>
    
    <div style="display:none;" id="myDiv" class="animate-bottom">
      <h2>Tada!</h2>
      <p>Some text in my newly loaded page..</p>
    </div>
    
    <script>
    var myVar;
    
    function myFunction() {
        myVar = setTimeout(showPage, 3000);
    }
    
    function showPage() {
      document.getElementById("loader").style.display = "none";
      document.getElementById("myDiv").style.display = "block";
    }
    </script>
    
    </body>
    </html>
    

    通过一些修改,可以提升UI体验!

    来源:W3 Schools

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-09
      • 1970-01-01
      • 2023-03-13
      • 2020-01-11
      • 1970-01-01
      • 2018-10-24
      • 2016-05-24
      • 1970-01-01
      相关资源
      最近更新 更多