我有一個上傳後呈現爲縮略圖的圖像列表。我的問題是我需要完全上傳的圖像的尺寸,以便我可以運行調整大小功能,如果大小不正確。jQuery/javascript - 在加載之前獲取圖像大小
功能是建立在圖像列表
function buildEditList(json, galleryID)
{
//Hide the list
$sortable = $('#sortable');
$sortable.hide();
for(i = 0, j = json.revision_images.length; i < j; i++) {
$sortable.append(
"<li id='image_" + json.revision_images[i].id + "'><a class=\"ui-lightbox\" href=\"../data/gallery/"
+ galleryID
+ "/images/album/"
+ json.revision_images[i].OrgImageName
+ "\"><img id=\""
+ json.revision_images[i].id
+ "\" alt=\"\" src=\"../data/gallery/"
+ galleryID
+ "/images/album/"
+ json.revision_images[i].OrgImageName
+ "\"/></a></li>"
).hide();
}
//Set first and last li to 50% so that it fits the format
$('#sortable li img').first().css('width','50%');
$('#sortable li img').last().css('width', '50%');
//Fade images back in
$sortable.fadeIn("fast",function(){
var img = $("#703504"); // Get my img elem -- hard coded at the moment
var pic_real_width, pic_real_height;
$("<img/>") // Make in memory copy of image to avoid css issues
.attr("src", $(img).attr("src"))
.load(function() {
pic_real_width = this.width; // Note: $(this).width() will not
pic_real_height = this.height; // work for in memory images.
});
alert(pic_real_height);
});
}
我一直在嘗試使用此stackoverflow後的解決方案,但還沒有得到它的工作,或者它可能只是我的代碼。如果你對此有任何想法,我可以使用幫助。目前該警報尚未定義。
聽起來像我會做服務器端的東西。 – Jeroen
請更具體地說明發生了什麼問題。 – Mark
@Mark - 目前我的警報(pic_real_width)未定義。 – Bungdaddy