javascript - 加载图像、javascript 文件后预加载 div?

标签 javascript jquery html

我一直在尝试使用 jQuery 库来预加载内容,我遇到的常见问题是,一旦所有内容都被预加载,它就会显示一个 div,但是在几秒钟内你仍然可以看到“看起来很糟糕”的内容作为 javascript还没有应用到它,然后它改变了它应该看起来的样子。很难解释。基本上我试图在加载整个页面后在页面上显示 div ( <div class="text"></div> ),所以图像、javascript 文件等。我发现了我不想在这里看到的很好的例子

http://tympanus.net/codrops/collective/collective-58/

您可以看到网格/内容仅在所有内容都加载完毕后才会显示,在用户获得加载图像作为等待指示之前,这正是我试图实现的目标。我试着查看源代码,但似乎“预加载”脚本在其他脚本中的某处,并且有很多不必要的代码(对我来说)。那么您能否为这个问题建议一个轻量级的 jQuery 解决方案?

最佳答案

最初对您的 div 使用 display:none 并显示加载 gif。

$(window).load(function() { 
   $('#yourGifID').hide();
   $('#yourDivID').show(); // will be called when everything is loaded
});

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images and sub-frames have finished loading.

关于javascript - 加载图像、javascript 文件后预加载 div?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15928575/

相关文章:

javascript - 对象的创建和执行阶段发生了什么?

javascript - 失去焦点时使用 qtip 应该会消失

html - 网页格式问题

javascript - 如何通过javascript清除输入类型="datetime"

javascript - 倒数计数器,用户可以在其中输入结束日期

javascript - 将未知数量的参数传递给 js 构造函数

php - 从 php 和 mysql 创建图表(折线图或条形图)

来自不同作用域变量的 javascript 回调函数未定义

html - 为什么垂直对齐 flexbox (1 :1 copy of MDN example) not working in Chrome?

jquery - 动态内容 CSS 样式与静态内容不匹配