• js图片未加载完显示loading效果


    <html>
    <title>js图片未加载完显示loading效果</title>
    <body>
    <style>
    img{float:left;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].style = "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>
  • 相关阅读:
    x64 平台开发 Mapxtreme 编译错误
    hdu 4305 Lightning
    Ural 1627 Join(生成树计数)
    poj 2104 Kth Number(可持久化线段树)
    ural 1651 Shortest Subchain
    hdu 4351 Digital root
    hdu 3221 Bruteforce Algorithm
    poj 2892 Tunnel Warfare (Splay Tree instead of Segment Tree)
    hdu 4031 Attack(BIT)
    LightOJ 1277 Looking for a Subsequence
  • 原文地址:https://www.cnblogs.com/cs122/p/10156562.html
Copyright © 2020-2023  润新知