关于图片预载功能

2011-08-25 15:11:07 by 【6yang】, 153 visits, 收藏 | 返回

<!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=utf-8" />
<style type="text/css" media="all">
div{border:#aaaaaa 3px solid;width:200px;padding:2px;margin:2px 9px;font-size:12px;line-height:22px;color:#999999;}
.ipt1{width:160px;font-size:12px;color:#1F6478;border:#999999 1px solid;margin-left:9px;}
.ipt2{border:#999999 1px solid;margin-left:6px;color:#666666}
p{margin:0px;padding:0px;background-image:url(http://www.wxwdesign.cn/attachments/month_0809/v2008925193118.gif);background-position:center;background-repeat:no-repeat;width:200px;height:200px;text-align:center;font-size:12px;color:#999999;line-height:26px;}
</style>
<script language="javascript" type="text/javascript">
function preloadimg(url,obj,ipt){
    var img=new Image();
    obj.innerHTML="<p>图片加载中...</p>";
    img.onload=function(){
        obj.innerHTML="";
        obj.style.width=String(img.width)+"px";
        ipt.style.width=String(img.width-40)+"px";
        obj.appendChild(img);
    };
    img.onerror=function(){
        obj.innerHTML="图片加载错误,请检查网络或URL地址!"
    };
    img.src=url; //img.src一定要写在img.onload之后,否则在IE中会出现问题
}
function show(){
    var div=document.getElementsByTagName("div")[0];
    var input=document.getElementsByTagName("input");
    preloadimg("flash5/images/buttom_2.jpg",div,input[0]);
    input[0].onclick=function(){this.value=""};
    input[1].onclick=function(){preloadimg(input[0].value,div,input[0]);}
}
window.onload=show;
</script>
<title>JavaScript获取图片大小——wxwdesign.cn</title>
</head>
<body>
<div></div>
<br />
<input type="text" value="请输入图片URL地址" class="ipt1"/><input type="button" value="加载" class="ipt2"/>
</body>
</html>

分享到:
share

    图片原图

    loading

    loading