Last active
February 4, 2020 06:07
-
-
Save schollz/ecc814acfa546721acdb9e55107b7277 to your computer and use it in GitHub Desktop.
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
// License: WTFPL | |
<div id="loadingMask" style="width: 100%; height: 100%; position: fixed; background: #fff;"></div> | |
<script> | |
function fadeOut(el) { | |
el.style.opacity = 1; | |
var last = +new Date(); | |
var tick = function() { | |
el.style.opacity = +el.style.opacity - (new Date() - last) / 80; | |
last = +new Date(); | |
// console.log(el.style.opacity); | |
if (el.style.opacity > 0) { | |
(window.requestAnimationFrame && requestAnimationFrame(tick)) || setTimeout(tick, 16); | |
} else { | |
el.style.display='none'; | |
} | |
}; | |
tick(); | |
} | |
function ready(fn) { | |
if (document.attachEvent ? document.readyState === "complete" : document.readyState !== "loading") { | |
el = document.getElementById('loadingMask'); | |
fadeOut(el); | |
var elements = document.querySelectorAll("img"); | |
Array.prototype.forEach.call(elements, function(el, i) { | |
if (el.getAttribute("alt")) { | |
const caption = document.createElement('figcaption'); | |
var node = document.createTextNode(el.getAttribute("alt")); | |
caption.appendChild(node); | |
const wrapper = document.createElement('figure'); | |
wrapper.className = 'image'; | |
el.parentNode.insertBefore(wrapper, el); | |
el.parentNode.removeChild(el); | |
wrapper.appendChild(el); | |
wrapper.appendChild(caption); | |
} | |
}); | |
} else { | |
document.addEventListener('DOMContentLoaded', fn); | |
} | |
} | |
window.onload = ready; | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment