我有一个脚本使用此函数来调整onLoad和onResize上的图像:
/**
* Calculates the display width of an image depending on its display height when it is resized.
* @param displayHeight the resized height of the image
* @param originalHeight the original height of the image
* @param originalWidth the original width of the image
* @return the display width
*/
function getDisplayWidth(displayHeight, originalHeight, originalWidth){
var ratio = originalHeight/displayHeight,
res = Math.round(originalWidth/ratio) || 1000;
return res;
}
..但我不希望图像高度超过800px,事实上它甚至可以固定为800×530px大小。我试图将固定值返回res
,但似乎无法正常工作。
谢谢!
答案 0 :(得分:2)
您只需要在函数中添加if语句......
/**
* Calculates the display width of an image depending on its display height when it is resized.
* @param displayHeight the resized height of the image
* @param originalHeight the original height of the image
* @param originalWidth the original width of the image
* @return the display width
*/
function getDisplayWidth(displayHeight, originalHeight, originalWidth){
if (displayHeight > 800) displayHeight = 800;
var ratio = originalHeight/displayHeight,
res = Math.round(originalWidth/ratio) || 1000;
return res;
}
设置宽度时,它会自动将高度设置为宽度/高度比的正确值。您还可以通过将变量传递给getDisplayWidth来获取高度,然后当函数返回时,该变量将具有高度,如最大高度条件所定义。