存在的问题为中心:大小调整图像以适合在一个div +保持比+水平和垂直方向+圆角
- 大小调整图像以适应在一个div - 解决
- 保持比 - 解决
- 水平,垂直居中 - 解决
圆角
一个)的矩形图像 - 解决
b)banner images - !!! impossiburu !!!
所以问题是:如何摆脱图像的矩形角?请看看这里,看看问题:>>>http://jsfiddle.net/infoman/5fzET/3/ < < <
换句话说:图像的角落是圆的,但他们不是在div的结束,而是超越它。我需要在div结束的行处对图像进行四舍五入。
HTML
test image ratio w/h = 4
<div>
<img id="myimg" src='http://placehold.it/200x50' draggable="false"/>
</div>
<br/>
test image ratio w/h = 0.25
<div>
<img id="myimg" src='http://placehold.it/50x200' draggable="false"/>
</div>
<br/>
test image ratio w/h = 1 but small
<div>
<img id="myimg" src='http://placehold.it/50x50' draggable="false"/>
</div>
<br/>
test image ratio w/h = 1 perfect fit
<div>
<img id="myimg" src='http://placehold.it/300x300' draggable="false"/>
</div>
<br/>
test image ratio w/h = 1 much too big
<div>
<img id="myimg" src='http://placehold.it/2000x2000' draggable="false"/>
</div>
CSS
div {
border: solid 1px green;
width: 300px;
height: 300px;
overflow: hidden;
border-radius: 10px;
}
div img {
outline: solid 1px red;
}
.fillwidth {
width: 100%;
height: auto;
position: relative;
/*top*/
}
.fillheight {
height: 100%;
width: auto;
position: relative;
/*left*/
}
.fillfull {
height: 300px;
width: 300px;
}
jQuery的
$('img[id^="myimg"]').each(function() {
var imgWidth = $(this).width(),
imgHeight = $(this).height(),
imgRatio = imgWidth/imgHeight,
imgWidthTop = (((300/imgWidth) * imgHeight)/2) * -1 + 300/2,
imgHeightLeft = (((300/imgHeight) * imgWidth)/2) * -1 + 300/2;
switch (true) {
case (imgRatio === 1):
$(this).addClass('fillfull');
break;
case (imgRatio < 1):
$(this).addClass('fillwidth');
$(this).css('top', imgWidthTop);
break;
case (imgRatio > 1):
$(this).addClass('fillheight');
$(this).css('left', imgHeightLeft);
break;
default:
break;
}
});
尝试&失败:
你能更清楚问题是什么? –
我没有看到问题。我也没有看到一个问题。你在问什么? – eh9
border-radius:10px 10px 10px 10px; – Bala