Lavish Sardana
Lavish Sardana

Reputation: 29

Preloader keeps on loading and doesnt disappear when the content is loaded

Ok so, first of all, Here's my code.

var loader = document.getElementById("loader");
window.addEventListener("loader", function () {
  loader.style.display = "none";
})
body {
  height: 100%;
  widows: 100%;
  overflow: hidden;
}

.loader {
  position: absolute;
  height: 80px;
  width: 80px;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  margin: auto;
  border: 6px solid lightgrey;
  border-radius: 100%;
  border-top: 6px solid skyblue;
  animation: spin 1s infinite linear;
}

@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
<div class="loader" id="loader"></div>
<h1>Hello World</h1>

Now the problem is that the content gets loaded but the spinner doesn't disappear. Any Solutions. BTW. This is my first post here so if you find that the code is not formatted correctly or some other mistake then forgive me.

Upvotes: 2

Views: 8348

Answers (4)

Shubham Dange
Shubham Dange

Reputation: 182

Loader is not event so that doesn't work same way you are thinking Create an element right below or end of loading process, and use jquery onload event on that Element, >> as process will done that Element will appear and it will trigger function.

Upvotes: 0

CodeJava
CodeJava

Reputation: 23

Does this work?(you have to download JQuery to use this)

window.onload(function() {
    $(“#loader”).css(“display”, “none”);
});

Upvotes: 0

Red
Red

Reputation: 7334

loader is not an event.

window.addEventListener ("loader", function() )

Change that to:

window.addEventListener ("load", function() {
    loader.style.display = 'none';
});

window.addEventListener ("load", fn()) waits until everything is loaded, including stylesheets and images.

The load event fires at the end of the document loading process. At this point, all of the objects in the document are in the DOM, and all the images, scripts, links and sub-frames have finished loading.

https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers/onload

Upvotes: 2

iLyas
iLyas

Reputation: 1091

You can use onload to hide the spinner when the page content had been loaded

To hide the spinner when the web page has completely loaded :

window.addEventListener ("load", function() {
    loader.style.display = 'none';
});

With Timeout :

//Hide the spinner after 2 seconds
setTimeout(function(){loader.style.display = 'none';}, 2000);

With Jquery :

$(window).load(function(){
   // PAGE IS FULLY LOADED  
  loader.style.display = 'none';
});

Demo Example :

var loader = document.getElementById('loader');

window.addEventListener ("load", function() {
    //Hide the spinner after 2 seconds
setTimeout(function(){loader.style.display = 'none';}, 2000);

});
body {
    height: 100%;
    widows: 100%;
    overflow: hidden;
}
.loader {
    position: absolute;
    height: 80px;
    width: 80px;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    border: 6px solid lightgrey;
    border-radius: 100%;
    border-top: 6px solid skyblue;
    animation: spin 1s infinite linear;
}
@keyframes spin {
    from{
        transform: rotate(0deg);
}
    to{
        transform: rotate(360deg);
}
}
<!DOCTYPE html>
<html>
<head>
<title>Loader Example</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div class="loader" id="loader"></div>
<h1>Hello World</h1>
</body>
</html>

Upvotes: 0

Related Questions