下面的代碼非常適合通過高度調整圖像大小的縱橫比,我還可以通過寬度創建單獨的函數。但我並不總是確定圖像是否需要通過高度或寬度來縮小。調整圖像大小
例如,如果圖像需要調整大小的空間是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