我有這樣的事情jQuery的()去更快的load()
function loadImages(){
var num = 1;
$('.myElement').each(function(){
var image = $("<img>").attr("src",mySrc).load(function(){
if(num == 1){
//do something - does not work
}
});
num++;
});
}
而且如果條件(NUM == 1)沒有在所有的工作。我認爲,這是因爲當第一個圖像被加載時,each()函數仍然有效,而var num有更大的值,因爲我的load()函數內的條件正在播放......我該如何解決它?
THX
全功能代碼
function loadThumbs(imageCount){
var perc = 0;
var cache = [];
var thumbHolderWidth = 0;
var thumbHolderHeight = 0;
$('#thumbs').find(".image_thumb").each(function(enumThumb){
if(enumThumb == 0){
$(this).addClass('active');
}
var thisThumbSrc = $(this).find('img').attr('src');
var smallim = $("<img>").attr("src",thisThumbSrc).load(function(){
var thumbWidth = this.width;
var thumbHeight = this.height;
thumbHolderWidth = thumbHolderWidth + thumbWidth + 12;
if(thumbHeight > thumbHolderHeight){
thumbHolderHeight = thumbHeight;
}
});
cache.push(smallim);
var imgSrc = $(this).attr('bigimg');
var im = $("<img>").attr("src",imgSrc).load(function(){
if(enumThumb == 0){
imWidth = this.width;
imHeight = this.height;
resizeOverlay(imWidth,imHeight,imgSrc);
}
perc = perc + (100/imageCount);
var loaderWidth = Math.round(winWidth*perc/100);
$('#thumb_loader').stop().animate({'width':loaderWidth+'px'});
if(Math.round(perc) >= 100){
$('#thumb_loader').animate({
'height':'1px',
'margin-top':'0px'
},'fast',function(){
$('#thumb_loader').addClass('loaded');
});
}
});
cache.push(im);
});
$('#images_overlay').find('#thumbs').css({
'width':thumbHolderWidth+'px',
'height':thumbHolderHeight+10+'px',
'left':winWidth/2-thumbHolderWidth/2+'px'
})
$('#images_overlay').find('#thumbs').fadeIn();
}
負載處理程序是否可以執行? – 2012-07-21 19:51:32
你的例子太設計了。 'num'代表什麼? (num ++應該在循環中嗎?) – pimvdb 2012-07-21 19:51:37
順便說一下,所有的加載處理程序都引用相同的'num'變量,該值始終爲2。 – 2012-07-21 19:52:33