Created
November 21, 2023 02:44
-
-
Save moshiurse/9409f939e3e05ea07fdf5f8e4310cab7 to your computer and use it in GitHub Desktop.
Image preloading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<div class="container"> | |
<div class="image"> | |
<div class="loader"><img src="loader.gif" alt="Loading..."></div> | |
<img src="https://images.unsplash.com/photo-1700406788223-2b053ac814ab?q=80&w=1972&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" /></div> | |
<div class="image"> | |
<div class="loader"><img src="loader.gif" alt="Loading..."></div><img src="https://plus.unsplash.com/premium_photo-1699534955989-26a13746d060?q=80&w=2071&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" /></div> | |
<div class="image"> | |
<div class="loader"><img src="loader.gif" alt="Loading..."></div><img src="https://images.unsplash.com/photo-1700410426386-704d39c87828?q=80&w=1974&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" /></div> | |
<div class="image"> | |
<div class="loader"><img src="loader.gif" alt="Loading..."></div><img src="https://images.unsplash.com/photo-1700422300144-713dad3a1c4a?q=80&w=1932&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D" /></div> | |
</div> | |
<script src="script.js"></script> | |
<!-- | |
This script places a badge on your repl's full-browser view back to your repl's cover | |
page. Try various colors for the theme: dark, light, red, orange, yellow, lime, green, | |
teal, blue, blurple, magenta, pink! | |
--> | |
<script src="https://replit.com/public/js/replit-badge-v2.js" theme="dark" position="bottom-right"></script> | |
</body> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
document.addEventListener("DOMContentLoaded", function () { | |
var images = document.querySelectorAll('.image > img'); | |
var loader = document.querySelectorAll('.loader'); | |
function hideLoader(index) { | |
loader[index].style.display = 'none'; | |
images[index].style.display = 'block'; | |
} | |
function loadImage(index) { | |
var img = new Image(); | |
img.onload = function () { | |
hideLoader(index); | |
}; | |
img.src = images[index].src; | |
} | |
for (var i = 0; i < images.length; i++) { | |
console.log(i) | |
loadImage(i); | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html { | |
height: 100%; | |
width: 100%; | |
} | |
.container { | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
flex-wrap: wrap; | |
/* height: 100vh; */ | |
} | |
.container div { | |
display: inline; | |
} | |
.container .image { | |
width: 300px; | |
height: 300px; | |
margin-bottom: 20px; | |
position: relative; | |
} | |
.image>img { | |
width: 100%; | |
height: 100%; | |
display: none; | |
} | |
.loader { | |
position: absolute; | |
top: 50%; | |
left: 50%; | |
transform: translate(-50%, -50%); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment