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>
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.
Share Improve this question edited Oct 17, 2020 at 13:25 Derek Wang 10.2k4 gold badges19 silver badges40 bronze badges asked Apr 23, 2018 at 14:08 Lavish SardanaLavish Sardana 291 silver badge5 bronze badges 2- move the var declaration of the loader inside the eventlistener – Temani Afif Commented Apr 23, 2018 at 14:11
- window.addEventListener ("loader", function() { var loader = document.getElementById('loader'); loader.style.display = 'none'; }) Is this right ? – Lavish Sardana Commented Apr 23, 2018 at 14:18
4 Answers
Reset to default 2loader
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/en-US/docs/Web/API/GlobalEventHandlers/onload
You can use onload
to hide the spinner when the page content had been loaded
To hide the spinner when the web page has pletely 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>
Does this work?(you have to download JQuery to use this)
window.onload(function() {
$(“#loader”).css(“display”, “none”);
});
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.