我使用下面的代码来检查图像的原始大小:
$(".post-body img").each(function() {
$(this).data('width', $(this).width());
$(this).data('height', $(this).height());
});在此之后,我将所有图像的大小调整为父div .post-body的宽度。
var bw = $('.post-body').width();
$(".post-body img").each(function() {
// Check the width of the original image size
if (bw < $(this).data('width')) {
$(this).removeAttr('height');
$(this).attr( 'width', bw );
//$(this).attr( 'height', height-in-ratio );
}
});这工作得很好!因此,我检查.post-body宽度是否小于图像的原始宽度,并将图像大小调整为与div相同的宽度。
然而,我认为这在Internet Explorer的一些老版本中是有缺陷的,因为我删除了图像的height属性。
如果存储了旧的宽度和高度,那么计算图像的比例高度的最简单方法是什么?我有一个新的宽度,我想计算适当的高度。
我似乎找不到一个简单的数学解决方案。
因此,我不想从图像中删除高度属性,但要计算成比例的高度。
谢谢你的小费。问候
发布于 2010-12-17 21:02:44
var newHeight = newWidth / oldWidth * oldHeight;newWidth / oldWidth提供了调整大小的比率。如果原始宽度为800,新宽度为400,则此比率为0.5。如果你的原始身高是600,你把它乘以这个比率,你会得到300。800x600与400x300的比率相同。
https://stackoverflow.com/questions/4470648
复制相似问题