I'm trying to set up a page loader for my website. Every time a user loads another page it will show the page loader until the page is done loading.
But for most of the cases, the web load so fast, and the page loader isn't shown. I tried to use setTimeout
for 5 seconds but it make the loader show endlessly.
Anyone know how to fix this?
Here's my code:
$(document).ready(function(){
setTimeOut(()=> {
$('.loader').remove();
} , 5000); // after 5 sec it will remove.
});
/*Loading Screen*/
.loader {
width: 100%;
height: 100%;
top: 0;
left: 0;
position: fixed;
display: block;
opacity: 0.8;
background-color:#221e1e;
z-index: 99;
text-align: center;
}
.loader img {
-webkit-animation: spin 2s linear infinite; /* Safari */
animation: spin 2s linear infinite;
position: absolute;
top: 50%;
left: 45%;
z-index: 100;
}
@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="loader">
<img src="img/logo/logo1.png" width="200px" height="75px">
</div>