我正在研究一些 jQuery 来调整页面上的图像大小。这个块工作正常:
var size = 350;
$("img").each(function () {
if ($(this).height() > $(this).width()) {
var h = size;
var w = Math.ceil(($(this).width() / $(this).height()) * size);
}
else {
var w = size;
var h = Math.ceil(($(this).height() / $(this).width()) * size);
}
$(this).css({ "height": h, "width": w });
});
问题是小图像被放大了。没问题,另外一个 if 语句应该可以解决这个问题!
var size = 350;
$("img").each(function () {
if ($(this).height() > size || $(this).width() > size) { //Always false
if ($(this).height() > $(this).width()) {
var h = size;
var w = Math.ceil(($(this).width() / $(this).height()) * size);
}
else {
var w = size;
var h = Math.ceil(($(this).height() / $(this).width()) * size);
}
$(this).css({ "height": h, "width": w });
}
});
我在哪里错了?