2016-11-25 7 views
0

현재 jQuery "addClass"및 "removeClass"를 사용하여 프리 로더를 제거하고 있습니다. 모든 웹 콘텐츠가로드 될 때 프리 로더 페이지를 제거하고 싶습니다. 시간 초과 기능을 설정하는 것은 정상적으로 작동하는 것처럼 보이지만 실시간 로딩과 같이 전체 사이트로드가 완료되면 동일한 작업을 수행 할 수없는 것처럼 보입니다. 어떤 도움을 주시면 감사하겠습니다.jQuery .load (function) 및 .addClass가 Preloader를 제거하기 위해 작동하지 않습니다.

//..HTML..// 

<div class="loader-wrapper"> 
</div> 

//..CSS..// 

.loader-wrapper { 
position: fixed; 
top: 0; 
left: 0; 
width: 100%; 
height: 100%; 
} 

.loaded .loader-wrapper { 
-webkit-transform: translateY(120%); 
-ms-transform: translateY(120%); 
transform: translateY(120%); 
-webkit-transition: all 1s 0.5s ease-out; 
transition: all 1s 0.5s ease-out; 
} 

//..jQuery..// 

<script> 
    $("body").removeClass('loaded'); 
</script> 

<script> 
$(window).load(function() { 
$("body").addClass('loaded'); 
}); 
</script> 


//..For reference this works perfectly..// 

<script> 
$(document).ready(function() { 

setTimeout(function() { 
$("body").addClass('loaded'); 
}, 1800) 

}); 
</script> 
+2

별도의 두 스크립트 태그에 JS 코드가있는 구체적인 이유는 무엇입니까? – George

+0

그것은 나에게 조금 깨끗해 보인다. –

+0

충분히 공정하고, 개인적으로 나는 그것을 추천하지 않을 것입니다. [반대하는 이유가 있습니다] (http://stackoverflow.com/questions/6451156/multiple-versus-single-script-tags)하지만 뭐든간에. 당신의 HTML은 어떤 모습이고 어떤 아약스 전화를하고 있습니까? – George

답변

2

모든 이미지가로드 될 때 확인하는 라이브러리를 사용할 수 있습니다. 예 : imagesLoaded.

그래서 코드처럼 보일 수 있습니다

// DOM is ready 
 
$(function() { 
 
    // We have to make sure that all of the images are loaded in the container 
 
    $('body').imagesLoaded(function() { 
 
     // Images are loaded 
 
     $('.loader').hide(); 
 
     $('img').show(); 
 
    }); 
 
});
img { 
 
    display: none 
 
}
<script src="https://code.jquery.com/jquery-3.1.1.js"></script> 
 
<script src="https://npmcdn.com/[email protected]/imagesloaded.pkgd.js"></script> 
 

 
<span class='loader'>Loading ...</span> 
 
<img width="200" src="https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a" />

+0

나는 또한 이것도 이전에 시도했는데 역시 효과가 없었습니다. –

+0

아마도 뭔가를 놓친 것일 수 있습니다. 작성자가 작동하는지 확인했기 때문에 여기에서 내 대답을 확인하십시오. http://stackoverflow.com/a/39938004/4312466 –

+0

참고 자료로 볼 수는 있지만 참조 용으로 편집하면 내 게시물이 편집되고 시간 초과 기능이 올바르게 작동합니다. –

-1
You can use the $(document).ready(function(){ // code }); 

를 완전히로드 웹 페이지를 대기하는 경우.

+0

[this] (http://stackoverflow.com/questions/5182016/what-is-the-difference-between-window-load-and-document-ready)를 읽으십시오. – George