下面的代码非常适合通过高度调整图像大小的纵横比,我还可以通过宽度创建单独的函数。但我并不总是确定图像是否需要通过高度或宽度来缩小。调整图像大小
例如,如果图像需要调整大小的空间是100宽度和100高度,图像是150乘以90,那么它的宽度需要缩小。如果图像是80乘160,那么高度需要缩小。
所以我问的是如何修改下面的代码,使它缩小图像的纵横比以适应宽度和高度的参数?谢谢。
jQuery.fn.resizeHeightMaintainRatio = function(newHeight){
if (this.aspectRatio == undefined)
this.aspectRatio = parseInt($(this).width()/$(this).height());
$(this).height(newHeight);
$(this).width(newHeight * this.aspectRatio);
}
我编辑的代码再次,由于在进一步的检查既不是我更新的版本也不丹的似乎正常工作。宽高比失去了,所以这里又是另一个。我已经在一张图片上尝试过,并且非常棒。这是,
jQuery.fn.resizeMaintainRatio = function(newHeight, newWidth){
widthRatio = parseInt($(this).width()/newWidth);
heightRatio = parseInt($(this).height()/newHeight);
if(heightRatio > widthRatio)
{
this.aspectRatio = parseInt($(this).css("width")/$(this).css("height"));
$(this).css("height", newHeight);
$(this).css("width", newHeight * this.aspectRatio);
}
else{
this.aspectRatio = parseInt($(this).css("height")/$(this).css("width"));
$(this).css("width", newWidth);
$(this).css("height", newWidth * this.aspectRatio);
}
}
注意AGAIN:经过进一步的使用上面的代码工作很奇怪,试试这个,而不是 - http://plugins.jquery.com/project/kepresize
不要忘记删除这一行:* if(this.aspectRatio == undefined)* – 2009-11-15 21:21:20