Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!--
- FULLSCREEN LOADING SCREEN SETUP
- ──────────────────────────────
- ♡ the HTML can be placed anywhere inside the <body>
- ♡ the <script> should be placed before </body>
- ♡ the CSS should go in your main stylesheet or inside a <style> tag
- -->
- <!-- =========================
- LOADER HTML
- =========================
- change the src to the link of your image
- (works as gifs or png, you can choose)
- -->
- <div id="loader-wrapper">
- <img src="/your-image.gif" alt="loading...">
- </div>
- <!-- =========================
- LOADER JAVASCRIPT
- =========================
- this script controls when the loader appears and disappears.
- BEHAVIOR:
- ♡ waits for the loader image to be ready
- ♡ shows the loader as soon as the image is loaded
- ♡ keeps it visible until the entire page finishes loading
- ♡ after page load:
- - waits 1.5 seconds
- - fades the loader out over 1 second
- IMPORTANT:
- ♡ you can change the value from 1500 to:
- - 1000 (disappears faster)
- - 3000 (stays longer)
- or just mess with the numbers until you're happy :3
- -->
- <script>
- const loader = document.getElementById('loader-wrapper');
- const img = loader.querySelector('img');
- const body = document.body;
- function showLoader() {
- loader.classList.add('active');
- body.classList.add('loading');
- }
- if (img.complete) {
- showLoader();
- } else {
- img.addEventListener('load', showLoader);
- }
- window.addEventListener('load', function() {
- setTimeout(() => {
- loader.classList.remove('active');
- setTimeout(() => {
- body.classList.remove('loading');
- }, 1000);
- }, 1500);
- });
- </script>
- <!-- =========================
- LOADER CSS
- =========================
- this CSS makes the loader:
- ♡ fixed to the viewport
- ♡ full width and height
- ♡ sit above all content
- ♡ fade in and out smoothly
- -->
- <style>
- #loader-wrapper {
- position: fixed;
- top: 0;
- left: 0;
- width: 100vw;
- height: 100vh;
- background: #000;
- z-index: 9999999;
- opacity: 0;
- transition: opacity 1s ease-in-out;
- pointer-events: none;
- display: block;
- }
- #loader-wrapper.active {
- opacity: 1;
- pointer-events: auto;
- }
- #loader-wrapper img {
- width: 100%;
- height: 100%;
- object-fit: cover;
- }
- </style>
- <!--
- not created by me!
- have fun~ :3
- -->
Advertisement
Add Comment
Please, Sign In to add comment