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>


 

ContractedBlock.gif ExpandedBlockStart.gif 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>


<id="photoholder">
<img src="http://hrbimg.focus.cn/upload/photos/190392/8u2zUVxw.jpg" alt="Picturesque fishing village on Ithaka." id="thephoto" />
</p>


</body>
</html>

 

转载于:https://www.cnblogs.com/mygood/articles/1489272.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值