怎么实现在页面加载完成之前显示loading

并且加载时间过长的话直接进入页面

阅读 10.1k
6 个回答
<!-- fixed满屏的遮罩,loading动画 -->
<div id="loading"></div>

document.addEventListener('DOMContentLoaded', function() {
  // Todo: 隐藏这个div的dom操作
});

setTimeout(function() {
  // Todo: 超时隐藏这个div
}, 1000);

<div id="loading"></div>


<style>

#laoding{
    width:100%;
    height: 100%;
    position:fixed;
    background: url("你得图片") no-repeat 100% 100%;
}

</style>


<script>

document.addEventListener('DOMContentLoaded', function() {
  var id = document.getElementById("loading");
  id.style.display = "none";
});

</script>

页面盖一个铺满屏幕的遮罩,fixed定位,然后DOMContentLoaded以后再关掉

document.onreadystatechange = function () {
if (document.readyState === "complete") {

}
}

分享一个我刚才看框架时学习到的“在内容加载前显示loading的效果”,这是模拟效果,你想要的是真实的内容加载完成前显示"loading"吧?

if (mui(".mui-loading")) {
    setTimeout(function(){
        //直接添加等待加载的内容,覆盖原来loading的位置
    },1000)
}
这是通过定时器模拟的效果,你需要的真实效果,应该可以通过load()事件监听吧
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题