【发布时间】:2015-12-06 19:01:18
【问题描述】:
所以我正在制作一个画廊,其中图像显示在 200x200 像素的列表项中
#gallery-list-ui li {
display: block;
margin: 10px;
height: 200px;
width: 200px;
overflow: hidden;
}
为了让纵向/横向图像使用整个高度/宽度,我使用 javascript 来决定是否应该将高度或宽度设置为 100%。
var portrait = ($img.height() > $img.width() ? true : false);
if(portrait){
$img.css({'width' :'100%', 'height' : 'auto'});
} else {
$img.css({'height' :'100%', 'width' : 'auto'});
}
结果是肖像图像正确缩放
<img src="img.jpg" style="width: 100%; height: auto;">
但横向图像会按其原始高度的 100% 缩放,并且不限于列表框的高度。
<img src="img.jpg" style="height: 100%; width: auto;">
我在这里缺少什么?谁能解释为什么会这样? 在这里查看我的小提琴:https://jsfiddle.net/smyhbckx/5/
【问题讨论】:
-
您的直接
img元素的父容器似乎没有定义height,因此不会阻止img显示完整height,但width有效,即使未设置,div默认为 100%。
标签: javascript css image-resizing