如果我明白你的問題正確,你想用CSS/HTML與height
和width
來調整圖像不拉伸的圖像。您想改爲修剪它。
我很確定您正在尋找CSS中的clip
屬性。混合clip
和width
和height
裁剪您的圖像,使其不被拉伸,但裁剪以適合所需的width
和height
。 Read up more about clip
here.
這裏是你要求爲你需要的額外的代碼(注意,它使用jQuery的,所以你需要嵌入這個工作):
<style>
.container { position: relative; overflow: hidden; }
.container img { position: absolute; }
</style>
<div class="container">
<img src="..."/>
</div>
<script type="text/javascript">
$image = $('.container img');
width = $image.width();
height = $image.height();
$image.css({
left: 0 - (width/2),
top: 0 - (height/2)
});
</script>
注意,我得到了第二個腳本從這個問題:Cropping images from center on the fly。
我很困惑,我真的不明白你的問題......你的代碼應該工作。 – 2013-01-13 22:28:51
@DumbSearch,但它顯然沒有,這就是問題':P' – tkbx
@tkbx我認爲他不希望它扭曲和拉伸,他希望它裁剪? – 2013-01-13 22:35:31