Utilize the Window: load event for optimal results.
The load event triggers upon complete page rendering, including all associated resources like stylesheets and images.
For a practical example, refer to the snippet below.
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#loader {
height: 100vh;
width: 100%;
background-color: black;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
z-index: 999;
}
#loader img {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 30%;
}
</style>
<title>The Loader</title>
</head>
<body>
<!-- Div for loader placement -->
<div id="loader">
<img src="loader.gif" />
</div>
<div>
<span>Your inquiry will be addressed shortly!</span>
</div>
</section>
<script>
//conceal the loader once the page is fully loaded
var loader = document.getElementById("loader");
window.addEventListener("load", function () {
loader.style.height = "100%";
loader.style.width = "100%";
loader.style.borderRadius = "50%";
loader.style.visibility = "hidden";
});
</script>
</body>
</html>