JS预加载图片

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS图片预加载</title>
<style type="text/css">
<!--
body{background:#000; color:#FFFFFF;  }
.big-img{height: 390px; text-align:center;  }
ol,ul{list-style:none}
ul{width: 632px; height: 56px; margin:10px auto;  }
li{float:left;  width: 200px; height: 53px; margin-right:10px; }
ul li img{ cursor:pointer}
-->
</style>
</head>

<body>
<div class="big-img"><img id="showImg" width="500" height="375" src="" alt="" /></div>
<ul id="imgList">
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s5.jpg" /></li>
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s6.jpg" /></li>
 <li><img src="http://www.codefans.net/jscss/demoimg/wall_s7.jpg" /></li>
</ul>
<script type="text/javascript">
 var imgL=document.getElementById("imgList").getElementsByTagName("img");
 var imgURL="http://www.codefans.net/jscss/demoimg/";
 var bigImg=new Array("wall5.jpg","wall6.jpg","wall7.jpg");
 var imgshow=new Image();
 for (var i = 0; i < imgL.length; i++) {
     (function() {
         var p = i

         imgL[i].onclick = function() {
             document.getElementById("showImg").src = this.src;
             imgshow.src = imgURL + bigImg[p];
             imgshow.onload = function() {
                 document.getElementById("showImg").src = this.src;
             }
         }
     })()
 }
</script>
</body>
</html>


 

Code

 

posted @ 2009-05-26 02:51  Mating_luo  阅读(253)  评论(0编辑  收藏  举报