javascript - 如何在页面加载前显示动画?
问题描述
我正在尝试显示来自https://lottiefiles.com/的等待动画
我创建了一个 JavaScript 函数,页面 0 上的一个区域,并使用动态操作调用该函数,它可以工作,但会显示所有区域的动画。
功能:
function createLoadAnimation() {
document.getElementById("LoadAnimation1").innerHTML = "";
const anim2 = lottie.loadAnimation({
container: document.getElementById('LoadAnimation1'),
path: `https://assets10.lottiefiles.com/packages/lf20_wd6xyqkx.json`,
renderer: 'svg',
loop: false,
autoplay: true,
});
};
用户等待页面加载时如何显示动画?
解决方案
您需要将加载程序添加到页面并最初显示它。然后一旦页面加载完成,您需要尽快隐藏加载器。
HTML:
</head>
<body onload="removeLoader();">
<!-- Loader here -->
<div id="loader"><p id="loader-content">Loading...</p></div>
<div>Inner content here</div>
</body>
</html>
CSS:
html {
padding: 0;
margin: 0;
height: 100%;
}
#loader {
justify-content: center;
align-items: center;
display: flex;
position: fixed;
top: 0;
left: 0;
width: 100vw;
min-height: 100vh;
padding: 0;
margin: 0;
z-index: 99999;
background: #f30
}
#loader p {
text-align: center;
color: #fff;
font-weight: bold;
font-size: 100px;
padding: 0;
margin: 0;
}
JS:
function removeLoader(){
setTimeout(()=>{
let loader = document.getElementById('loader');
// hide the loader
loader.style = 'display: none;';
},
1000);
}
推荐阅读
- javascript - 在 Officejs 加载项中提交文件时如何处理 getFileAsync 失败状态
- sql - SQL DB2 根据计数拆分分组结果
- matplotlib - 未保存数字
- python-3.x - 从对角线创建 3-D(或更大)对角线 NumPy 数组
- bash - Bash 中的双向哈希?
- python - Azure Functions/Cosmos DB 中的 MaxItemCount
- jenkins - 减少并行运行多个作业的管道脚本
- sql - 当我尝试将日期转换为 char 并且时间戳部分被删除时
- c# - 尝试钛网络代理解决方案
- java - java.lang.NullPointerException:尝试在从服务器到微调器的空对象引用上调用接口方法“int java.util.List.size()”