www.BCZJ.com【编程之家】 >> 网页制作 >> 为你的网页加一个Loading

为你的网页加一个Loading

www.BCZJ.com【编程之家】 www.bczj.com admin 2007-2-22 14:04:20

熟悉Flash的朋友都知道一般比较大的Flash作品都要加Loading以防止其播放不连续。相比之下网页就小的多,但是如果在网速很慢的情况下,打开一个10KB左右的网页的时间要超过30秒,这时就有必要加上Loading了。

Loading的原理很简单,在网页中加入一个不透明的层,z-index属性设为“100”,即使它处在最上一层也不例外,width、height都设为“100%”。当网页加载完毕触发onload事件时会将它隐藏。具体的代码如下:

<!doctype html public "-//W3C//DTDHTML 4.0 Transitional//EN">

<html>

<head><title>为网页加Loading

</title>

<meta http-equiv="Content-Type"

content="text/html; charset=gb2312">

</head>

<body onload='clearInterval(timer);Loading.style.display="none";'>

<div id="Loading" style="position:absolute;width:100%; height:100%;z-index:100;background:white;">

<div id="Waiting" style="position:absolute;left:40%;top:50%;"></div></div>

<script>

var text="正在载入文档";i=0;

function load() {

if (i<6) {

text+=".";

Waiting.innerText=text;

i++;}

else {text="正在载入文档";i=0;}}

timer=window.setInterval("load()",300);

</script>

<!--以下是网页的内容.-->

<img src="http://ent.omeweb.com/book/content/2005/04/http://图片地址>"

</body>

</html>

OK!Loading就做完了,试一下吧,不过值得注意的是当文档中只有<script>和<iframe>标记时Loading可能不起作用。

责任编辑: 参与评论
【编程之家】版权所有 www.bczj.com 沪ICP备06025676号 QQ:382611662
Powered By: 上海驰志网络 3.0 Beta