我正在研究一些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 });
});
問題是小圖像被放大。沒問題,如果陳述應該照顧的話,還有一個問題!
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 });
}
});
我在哪裏錯了?
圖像的大小(高度/寬度)是多少? – 0x499602D2
您是否嘗試「提醒()」這些值?也許他們是'空'? –
圖像有各種尺寸。該源代碼是chat.stackoverflow.com中的已知郵件的抓取。所以無論人發佈什麼。 – Billdr