2012-10-29 5 views
1

커다란 배경 이미지가 포함 된 큰 페이지가 생성되므로 프리 로더를 표시하고 백그라운드에서 이미지를로드하고 로더를 업데이트하는 것이 가장 좋습니다 (이미지 4/20 , ecc). 내 문제는 : onload 이벤트가 발생하면 내 로더 diasppears하지만 여전히 이미지로드를 볼, 이것은 완전히로드되지 않은 것을 의미합니다. 왜 이런 일이 생길까요? 어떤 생각?image onload trouble

$(document).ready(function(){ 
// This will be executed when the DOM is ready. 
}); 

$(window).load(function(){ 
// This will be executed when the whole document and all its 
// referenced items (style sheets, scripts, images, etc.) are loaded. 
}); 

그러나 :

nImages = $('.slide').length; 
loadedImgs = 0; 
var bgImages = ['img/bbb.png','img/bbb2.png','img/bbb3.png']; 

$('.slide').each(function(i){ 
    var curSlide = $(this); 
    var img = new Image(); 
    img.src = bgImages[ i % 3 ]; 
    img.onLoad = imageLoaded(img, curSlide); 
})  

function imageLoaded(img, curSlide){ 
    curSlide.css('backgroundImage', 'url(' + img.src + ')'); 
    loadedImgs++; 
    if(nImages == loadedImgs){ 
     $('#container').css('visibility','visible'); 
     $('#loader-cont').fadeOut(1000); 
    } 
    $('.loader-inner .title').text(loadedImgs/nImages); 
} 
+1

대신에 onready를 사용하십시오. – NotGaeL

+0

또한 패턴은 img.onload = someFunc입니다. _THEN_ img.src = someFile; 로드하기 전에 파일이로드 될 때 수행 할 작업을 지정해야합니다. 이미지가 이미 캐시 된 경우 쉽게 onload 이벤트가 발생하지 않을 수 있습니다. 그것은 (대부분) 작동 할 수도 있지만 올바른 접근 방식은 아닙니다. – enhzflep

답변

1

당신은 창로드와 document.ready 몇 가지 문제를 갖고있는 것 같다 사전에

http://zhereicome.com/experiments/statics/myascensor/#1-1

감사 : 여기

페이지의 이 질문은 이전에 많이 묻고 많은 답변이있었습니다. How can I create a "Please Wait, Loading..." animation using jQuery?

+2

감사합니다 elcodedocle,하지만로드 애니메이션을 추가하고 창로드에서 제거 내가 뭘 찾고 있지 않습니다. 내가 원한 것은 백분율로 로딩 화면이었습니다. 어쨌든, 나는 다음과 같이 해결 : $ (IMG) .load (함수() { curSlide.css ('backgroundImage의', 'URL ('+ img.src + ')'); loadedImgs ++; (nImages 경우 fadeOut (1000); } $ ('.loader-cont') { $ ('컨테이너') .css ('가시성', '표시'), $ text ('loading ...'+ ((loadedImgs/nImages) * 100) + '%'); }}) –