javascript图片大小处理函数
<script language=Javascript>
var proMaxHeight = 150;
function proDownImage(ImgD){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
var rate = (proMaxWidth/image.width < proMaxHeight/image.height)?proMaxWidth/image.width:proMaxHeight/image.height;
if(rate <= 1){
ImgD.width = image.width*rate;
ImgD.height =image.height*rate;
}
else {
ImgD.width = image.width;
ImgD.height =image.height;
}
}
}
//-->
<img src="http://tech.ddvip.com/%22http://www.baidu.com/images/logo.gif/%22" />
这段代码可以根据网友上传的图片,进行比例缩放。这样不会导致很多商品展示都出现的。图片尺寸都固定死了的情况
用法很简单,把上面的这段javascript放到<head></head>之间,
然后把<img src="http://tech.ddvip.com/%22http://www.baidu.com/images/logo.gif/%22" />
改成<img src="http://tech.ddvip.com/%22http://www.baidu.com/images/logo.gif/%22" onload=proDownImage(this); />这样。就可以了。
意思就是在图片load装载的时候,调用proDownImage函数对this(即图片本身)对象进行设置。

浙公网安备 33010602011771号