js图片未加载完显示loading效果
<html> <title>js图片未加载完显示loading效果</title> <body> <style> img{float:left;width:200px;height:200px;margin:0 10px 10px 0} </style> <script> //判断浏览器 var Browser=new Object(); Browser.userAgent=window.navigator.userAgent.toLowerCase(); Browser.ie=/msie/.test(Browser.userAgent); Browser.Moz=/gecko/.test(Browser.userAgent); //判断是否加载完成 function Imagess(url,imgid,callback){ var val=url; var img=new Image(); if(Browser.ie){ img.onreadystatechange =function(){ if(img.readyState=="complete"||img.readyState=="loaded"){ callback(img,imgid); } } }else if(Browser.Moz){ img.onload=function(){ if(img.complete==true){ callback(img,imgid); } } } //如果因为网络或图片的原因发生异常,则显示该图片 img.onerror=function(){img.src="http://www.86y.org/images/failed.png"} img.src=val; } //显示图片 function checkimg(obj,imgid){ document.getElementById(imgid).style.cssText=""; document.getElementById(imgid).src=obj.src; } //初始化需要显示的图片,并且指定显示的位置 window.onload=function(){ var imglist=document.getElementById("imagelist").getElementsByTagName('img'); for(i=0;i<imglist.length;i++) { imglist[i].id="img0"+i; //imglist[i].src="http://www.86y.org/images/loading.gif"; imglist[i].style.cssText="background:url(http://www.86y.org/images/loading.gif) no-repeat center center;" Imagess(imglist[i].getAttribute("data"),imglist[i].id,checkimg); } } </script> <div id="imagelist"> <img data="http://www.smashingapps.com/wp-content/uploads/2013/05/uikitsforwebmobile17s.jpg" /> <img data="http://www.smashingapps.com/wp-content/uploads/2013/05/uikitsforwebmobile13.jpg" /> <img data="http://www.smashingapps.com/wp-content/uploads/2013/05/uikitsforwebmobile39.jpg" /> <img data="http://www.smashingapps.com/wp-content/uploads/2013/05/uikitsforwebmobile10.jpg" /> <img data="http://www.smashingapps.com/wp-content/uploads/2013/05/uikitsforwebmobile14.jpg" /> </div> </body> </html>