<!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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Onload image fades without Flash | clagnut/sandbox</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<style type="text/css">
#photoholder {
width:450px;
height:338px;
border:1px solid #666;
background:#fff url('http://www.cssrain.cn/demo/Onloadimage/indicator_web20_working.gif') 50% 50% no-repeat;
}
#thephoto {
width:450px;
height:338px;
}
TABLE {
margin-bottom:1em;
border-collapse:collapse;
}
TH {
font-weight:normal;
font-style:italic;
white-space:nowrap;
text-align:left;
}
TD, TH {
vertical-align:top;
padding:0.25em 0.5em;
border:1px solid #ddd;
}
</style>
<script type="text/javascript">
<!--
document.write("<style type='text/css'>#thephoto {visibility:hidden;}</style>");
function initImage() {
imageId = 'thephoto';
image = document.getElementById(imageId);
setOpacity(image, 0);
image.style.visibility = "visible";
fadeIn(imageId,0);
}
function fadeIn(objId,opacity) {
if (document.getElementById) {
obj = document.getElementById(objId);
if (opacity <= 100) {
setOpacity(obj, opacity);
opacity += 10;
window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 100);
}
}
}
function setOpacity(obj, opacity) {
opacity = (opacity == 100)?99.999:opacity;
// IE/Win
obj.style.filter = "alpha(opacity:"+opacity+")";
// Safari<1.2, Konqueror
obj.style.KHTMLOpacity = opacity/100;
// Older Mozilla and Firefox
obj.style.MozOpacity = opacity/100;
// Safari 1.2, newer Firefox and Mozilla, CSS3
obj.style.opacity = opacity/100;
}
window.onload = function() {initImage()}
// -->
</script>
</head>
<body>
<p id="photoholder">
<img src="http://hrbimg.focus.cn/upload/photos/190392/8u2zUVxw.jpg" alt="Picturesque fishing village on Ithaka." id="thephoto" />
</p>
</body>
</html>