如何防止徽标在加载时导致导航栏抖动
How to prevent logo from causing navigation bar from jittering while it's loading
一段时间以来我一直在努力寻找解决方案,但 none 似乎可行。
我在导航到网站上的任何和所有页面时都会遇到这个问题 - 这非常烦人。
虽然我希望加载站点图像需要一些时间,但此加载会影响我的导航栏和站点徽标的加载。在加载每个页面时,我网站的徽标完全不存在 - 这导致我的导航栏一直向上移动,直到出现徽标。这通常需要一瞬间,但也完全取决于用户的互联网连接。
如何防止这种情况发生?这会导致我的整个网站在导航时 "bounce",所有内容都在短时间内向上移动,而徽标不存在。
为您的图片标签指定一个绝对高度属性。这将使浏览器保持 img 标签的高度,并允许元素加载到正确的位置。
您也可以尝试调整加载器,使其仅在页面中的所有元素都已加载时才加载页面。就这么简单:
<!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
希望对您有所帮助!
一段时间以来我一直在努力寻找解决方案,但 none 似乎可行。
我在导航到网站上的任何和所有页面时都会遇到这个问题 - 这非常烦人。
虽然我希望加载站点图像需要一些时间,但此加载会影响我的导航栏和站点徽标的加载。在加载每个页面时,我网站的徽标完全不存在 - 这导致我的导航栏一直向上移动,直到出现徽标。这通常需要一瞬间,但也完全取决于用户的互联网连接。
如何防止这种情况发生?这会导致我的整个网站在导航时 "bounce",所有内容都在短时间内向上移动,而徽标不存在。
为您的图片标签指定一个绝对高度属性。这将使浏览器保持 img 标签的高度,并允许元素加载到正确的位置。
您也可以尝试调整加载器,使其仅在页面中的所有元素都已加载时才加载页面。就这么简单:
<!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
希望对您有所帮助!