利用js获取图片尺寸与图片大小(高度与宽度)

利用获取图片尺寸与图片大小(高度宽度)要注意一点的是要等 图片加载完成后才能js 获取图片宽度与高度的,所以要判断在readystate=="complete"的状态下获取大小,如果是利用file上传的话,每次都要点击清除image=new image();  imgage.width与高度哦。
<!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>
<script>
function checkimg(img)

{
 var message="";
 var maxwidth=1;//设置图片宽度界限
 var maxheight=1;//设置图片高度界限

 if(img.readystate!="complete"){

  return false;//确保图片完全加载

 }
 if(img.offsetheight>maxheight) message+="r高度超额:"+img.offsetheight;
 if(img.offsetwidth>maxwidth) message+="r宽度超额:"+img.offsetwidth;
 if(message!="") alert(message);
}

</script>
</head>
<body>
<img src="http://jz345.net/images/frequency.gif" border=0 id="img1" οnlοad="checkimg(this);">
<br> <input id=inp type="file" onpropertychange="img1.src=this.value;">
</body>
图片加载完成后才能成功获取图片大小,所以要判断在readystate=="complete"的状态下获取大小

测试代码
<script>
function qford_getimagesize(filepath)  
    {  
        var imgsize={  
        width:0,  
        height:0  
        };  
         image=new image();  
        image.src=filepath;  
        imgsize.width =image .width;  
        imgsize .height=image .height;  
        return imgsize;  
    }  

</script>

js获取图片尺寸函数测试:</br>  
<img src="test.jpg" mce_src="test.jpg" />  
<script type="text/网页特效"><!--  
var a=qford_getimagesize("test.jpg");  
alert('宽:'+a.width +'  高:'+a.height);
</script>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值