2

我有一个脚本,它使用这个函数来调整图像 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但它似乎不起作用。

谢谢!

4

1 回答 1

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 来获取高度,然后当函数返回时,该变量将具有由最大高度条件定义的高度。

于 2012-07-17T12:48:07.203 回答