Lord Eww Lel Lord Eww Lel - 8 months ago 55
Javascript Question

jQuery .load(function) and .addClass in Order to Remove Preloader is Not Working

I am currently using jQuery "addClass" and "removeClass" to remove the preloader. I want the preloader page to be removed when all web content is loaded. Setting a timeout function seems to work fine but I cannot seem to get to do the same when the entire site is finished loading, as in real time loading. Some help is highly appreciated.


<div class="loader-wrapper">


.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;



$( window ).load(function() {

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

$(document).ready(function() {

setTimeout(function() {
}, 1800)


Answer Source

You can use a library, that validates when all images are loaded. For example imagesLoaded.

So your code may look something like that:

// 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
img {
    display: none
<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://npmcdn.com/imagesloaded@4.1/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" />