如果我正确理解了您的问题,您想使用 CSS/HTML 调整图像大小height
,width
有无拉伸图像。你想裁剪它。
我很确定您正在寻找clip
CSS 中的属性。混合clip
和width
来height
裁剪图像,使其不会被拉伸,但会被裁剪以适合所需的width
和height
。阅读更多关于clip
这里。
这是您根据需要请求的额外代码(请注意,它使用 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。