【发布时间】:2013-07-24 22:32:00
【问题描述】:
我有以下代码:
function fn_get_natural_dim(){
var width = this.width;
var height = this.height;
var ratiow = width/600;
var ratioh = height/300;
$("#output").append(img_name,",");
if(ratiow>=ratioh)
{
height = height/ratiow;
$(slide_image).css("width","600px");
$(slide_image).css("height",height);
var margin = (300-height)/2;
$(slide_image).css("margin-top",margin);
}
else
{
width = width/ratioh;
$(slide_image).css("width",width);
$(slide_image).css("height","300px");
var margin = (600-width)/2;
$(slide_image).css("margin-left",margin);
}
}
var max_count = $(".slider").children().length;
for(var count=1;count<=max_count;count++)
{
var count_string = count.toString();
var img_name = "img" + count_string;
var slide_image = document.getElementById(img_name);
var img = new Image();
img.onload = fn_get_natural_dim;
img.src = $(slide_image).attr("src");
}
});
对于每张图片(我目前有三张),我会得到它的自然尺寸,这样我就可以计算出适合 600x300 div 所需的边距。
但我只适用于最后一个,即第三个。 换句话说,在#output 中,它显示“img3,img3,img3”(由 $("#output").append(img_name,","); 调用) 如何让它显示“img1,img2,img3”??
【问题讨论】:
-
不到一小时前你问了同样的问题。
-
@Barmar 这不是完全相同的问题 - 这段代码(在某种程度上)用我对上一个问题的答案进行了修复。现在 OP 有一个不同的问题。
-
@Alnitak 是的,我有点过分了。
标签: javascript jquery function for-loop