首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >设置最大高度onResize

设置最大高度onResize
EN

Stack Overflow用户
提问于 2012-07-17 20:40:56
回答 1查看 130关注 0票数 2

我有一个脚本,它使用这个函数来调整图像onLoad和onResize的大小:

代码语言:javascript
复制
/**
 * 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,但似乎不起作用。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-07-17 20:48:07

你只需要给你的函数添加一条if语句...

代码语言:javascript
复制
/**
 * 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来获取高度,然后当函数返回时,该变量将具有由最大高度条件定义的高度。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11522646

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档