javascript - 预加载 JavaScript

标签 javascript jquery

我正在尝试制作预加载器,我当前有此代码,但它在页面加载之前没有显示预加载器。

$(document).ready(function(){
        $(".preloader-wrapper").show();
        $("body").hide();
     });

$(window).load(function(){
  $(".preloader-wrapper").fadeOut("slow", function(){
    $("body").fadeIn("slow");
  });
});

编辑:明白了。

setTimeout(function() {
  $('#preloader').fadeOut('slow', function() {
    $(this).remove();
  });
}, 2000);

#preloader {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  overflow: visible;
  background: #333 url('http://files.mimoymima.com/images/loading.gif') no-repeat center center;
}

最佳答案

您可以将其首先加载并放在顶部。然后在 dom 加载后将其删除即可。

setTimeout(function() {
  $('#preloader').fadeOut('slow', function() {
    $(this).remove();
  });
}, 2000);
#preloader {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 999;
  width: 100%;
  height: 100%;
  overflow: visible;
  background: #333 url('http://files.mimoymima.com/images/loading.gif') no-repeat center center;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="preloader"></div>
<h1>SUPER SIMPLE FULL PAGE PRELOADER</h1>

关于javascript - 预加载 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37146504/

相关文章:

javascript - 在 React 中使用 Link Component 时如何摆脱样式

javascript - Chrome 扩展程序 : Open new tab and click on button

jQuery serializeArray 不包括单击的提交按钮

javascript - 为什么在 jQuery 中使用回调函数来触发事件?

javascript - 模拟 div 内的 href 的点击

javascript - 当我使用 Internet Explorer 11 单击 href 链接时出现错误

javascript - 错误 : Module did not self-register.

javascript - 有没有办法检查 $.when(...) 中 promise 的各个结果,无论最终状态如何

jquery.load 不适用于包含部分 html 的文件

jquery - 使用 Fabric 缩放到 Canvas 中心