在Resize上设置最大高度
Setting a max height onResize
我有一个脚本,它使用这个函数来调整图像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来获得高度,然后当函数返回时,该变量将具有最大高度条件定义的高度。
相关文章:
- 将“潜水高度”设置为“另一潜水高度”
- 如何将自动高度设置为svg元素
- 匹配最高选项卡的高度 - 设置幻灯片高度
- 将文档高度设置为负 100px
- 如何将宽度和高度设置为可拖动图像
- 如何将每个 td 元素的高度设置为其宽度
- 高度设置为“自动”不适用于 JS 动画
- 如何获取图像的高度并将其父高度设置为自动
- 当高度设置为 100% 时,SWF 无法填充窗口
- 根据 jQuery 中多个实例上的另一个元素高度设置高度
- 将元素高度设置为等于主体高度减去 170px
- 在JavaScript中将宽度和高度设置为百分比
- Javascript/jQuery:当最大高度设置为0时获取高度
- 将facebook画布应用程序的高度设置为等于facebook侧边栏的高度
- 如何在页面加载时将剑道网格高度设置为固定值
- 使用jQuery展开DIV时,将高度设置为auto
- 如何将嵌套的DIV高度设置为与容器相同
- 是否可以将夏季注释的角度高度设置为父高度
- 如何将标签高度设置为内容高度
- js将画布宽度/高度设置为0,并且不显示任何内容