gpt4 book ai didi

php - 设置最大高度 onResize

转载 作者:行者123 更新时间:2023-11-29 22:22:37 26 4
gpt4 key购买 nike

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

谢谢!

最佳答案

您只需要在您的函数中添加一个 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 来获取高度,然后当函数返回时,该变量将具有由最大高度条件定义的高度。

关于php - 设置最大高度 onResize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11522646/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com