Line 371: | Line 371: | ||
<script type="text/javascript"> | <script type="text/javascript"> | ||
− | + | (() => { | |
− | + | 'use strict'; | |
− | + | // Page is loaded | |
− | + | const objects = document.getElementsByClassName('asyncImage'); | |
− | + | Array.from(objects).map((item) => { | |
− | + | // Start loading image | |
− | + | const img = new Image(); | |
− | + | img.src = item.dataset.src; | |
− | + | // Once image is loaded replace the src of the HTML element | |
− | + | img.onload = () => { | |
− | + | item.classList.remove('asyncImage'); | |
− | + | return item.nodeName === 'IMG' ? | |
− | + | item.src = item.dataset.src : | |
− | + | item.style.backgroundImage = `url(${item.dataset.src})`; | |
− | + | }; | |
− | + | }); | |
− | + | })(); | |
− | + | ||
</script> | </script> |
Revision as of 18:11, 16 October 2018