js判断图片加载完成

<!DOCTYPE>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>


</head>
<body>

<img id="mainPic" src="http://img1c.xgo-img.com.cn/pics/1843/b1842753.jpg" />


<script>

loadingImg('http://img1c.xgo-img.com.cn/pics/1843/b1842753.jpg',getWH);

function getWH(){
    set(this.width, this.height)
}

function loadingImg(url,fun){
    var _Img = new Image();
    _Img.src = url;
    /*判断是否已加载*/
    (_Img.complete) ? fun.apply(_Img,arguments) : _Img.onload = fun;
}

function set(w,h){
    console.log(w,h)
}
</script>
</body>
</html>

 

posted @ 2014-08-01 22:35  dtdxrk  阅读(281)  评论(0)    收藏  举报