我正在寻找一种方法来加载页面上的几个隐藏的缩略图(大约500),计算它们的总宽度,并在全部加载后显示它们的容器。用回调加载几个图像()
问题是容器在全部加载之前一直显示。
下面是简单的代码片段,我从我的脚本中提取:
// $('#thumbScroller') is the container, and is initially hidden.
var imgs = ['http://www.google.com/images/nav_logo95.png', 'http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=4'];
for(var i = 0; i < i.length; i++){
var url = imgs[i];
$('#thumbScroller').append('<img src="' + url + '" class="thumb" />');
// all elements were appened at this point
if(i == $this.totalImages-1){
//variable to hold total container width
var totalContent=0;
// loop through images to calculate total width
$('#thumbScroller img').each(function (s) {
totalContent += $(this).width();
//last image, show interface elements
if(s == $('#thumbScroller img').length-1){
$('#thumbScroller').width(totalContent).fadeIn();
};
});
}
}
任何帮助,将不胜感激!
将.hidden类添加到for之前的.interface元素。 –