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 に答える