隐藏内容加载 GIF
我在页面的 div 中有两个大图像文件,需要几秒钟才能加载。如何在出现“正在加载 gif”时隐藏正在加载的内容,然后在完全加载后显示该内容?
我对javascript一无所知,但我尝试使用这段代码。它完成了我想要它完成的一半任务。 “正在加载 gif”有效,但问题是加载时内容是可见的。
http://aaron-graham.com/test2.html
<div id="loading" style="position:absolute; width:95%; text-align:center; top:300px;">
<img src="img/parallax/ajax-loader.gif" border=0>
</div>
<script>
var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
if (ns4)
ld=document.loading;
else if (ns6)
ld=document.getElementById("loading").style;
else if (ie4)
ld=document.all.loading.style;
function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6||ie4) ld.display="none";
}
</script>
任何帮助将不胜感激!
I have two large image files in a div on a page that take several seconds to load. How can I hide the loading content while a "loading gif" appears and then have the content appear once it has fully loaded?
I don't really know anything about javascript but I tried using this code. It did half of what I wanted it to do. The "loading gif" worked but the problem was that the content was visible as it was loading.
http://aaron-graham.com/test2.html
<div id="loading" style="position:absolute; width:95%; text-align:center; top:300px;">
<img src="img/parallax/ajax-loader.gif" border=0>
</div>
<script>
var ld=(document.all);
var ns4=document.layers;
var ns6=document.getElementById&&!document.all;
var ie4=document.all;
if (ns4)
ld=document.loading;
else if (ns6)
ld=document.getElementById("loading").style;
else if (ie4)
ld=document.all.loading.style;
function init()
{
if(ns4){ld.visibility="hidden";}
else if (ns6||ie4) ld.display="none";
}
</script>
Any help would be greatly appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用jquery,代码如下: 使用
html:
它的工作原理是在调用文档的ready函数时运行(在构建DOM和其他资源之后调用),然后它将使用您设置的图像的url分配img的src属性想要。
将 nyquil.org 网址更改为您想要的图像,并根据需要添加尽可能多的图像(只是不要过度;)。已测试 Firefox 3/chrome 10。
这是演示: http://jsfiddle.net/mazzzzz/Rs8Y9/ 1/
Use jquery, with code like:
With the html like:
It works by running when document's ready function is called (which is called after the DOM and other resources have been constructed), then it will assign the img's src attribute with the image's url you want.
Change the nyquil.org url to the image you want, and add as many as needed (just don't go overboard ;). Tested Firefox 3/chrome 10.
Here is the demo: http://jsfiddle.net/mazzzzz/Rs8Y9/1/
在处理 HTML 结构时,我为这两个图像添加了一个
notifyLoaded
类,以便您可以通过onload
事件监视两个图像何时加载。由于 css 背景图像没有得到该事件,我使用背景路径创建了一个隐藏的 img,以便我们可以测试该图像何时加载HTML:
您已经在页面中引用了 jQuery,因此我将您的脚本替换为下列的。
jQuery:
我还将 #Vertical 设置为默认的
display:none;
,当图像加载CSS 时,它会发生变化:
Working off your HTML structure I added a
notifyLoaded
class for the two images so you can watch for when both have loaded via anonload
event. Since css background images don't get that event I've created a hidden img using the background's path so we can test when that image is loadedHTML:
You have reference to jQuery in your page already so I've replaced your script to the following.
jQuery:
I've also set #Vertical to a default
display:none;
which gets changed when images have loadedCSS: