Hi all, I was having an issue with my site showing a black flash or white flash when loading and I’m pretty sure it’s due to the large amount of content on my site. I found a solution from this linked post but had an issue where it would apply to the homepage, but not other pages and they’d would still have the flash before loading. I attached the code I used for my site that got rid of the flash on all pages of the site, hope it’s helpful.
HEAD CODE
<style>
html, body {
background-color: #fefefe !important;
}
#aka-preloader {
position: fixed;
inset: 0;
background: #fefefe;
z-index: 99999;
opacity: 1;
pointer-events: none;
transition: none; /* showing is instant */
}
#aka-preloader.is-hidden {
opacity: 0;
transition: opacity .5s cubic-bezier(.4, 0, .2, 1); /* hiding fades */
}
</style>
Before code:
<div id="aka-preloader"></div>
<script>
(function () {
var p = document.getElementById('aka-preloader');
if (!p) return;
var cancelPending = null;
function show() {
if (cancelPending) cancelPending();
p.classList.remove('is-hidden');
hideWhenSettled();
}
// Hide once the page has stopped changing (or after 2.5s max)
function hideWhenSettled() {
var quiet, obs;
var max = setTimeout(done, 2500);
function done() {
cancelPending && cancelPending();
p.classList.add('is-hidden');
}
function reset() {
clearTimeout(quiet);
quiet = setTimeout(done, 350);
}
obs = new MutationObserver(reset);
obs.observe(document.body, { childList: true, subtree: true });
reset();
cancelPending = function () {
obs.disconnect();
clearTimeout(quiet);
clearTimeout(max);
cancelPending = null;
};
}
// First load
if (document.readyState === 'complete') hideWhenSettled();
else window.addEventListener('load', hideWhenSettled);
// Catch every in-site navigation (links, menus, buttons)
['pushState', 'replaceState'].forEach(function (fn) {
var orig = history[fn];
history[fn] = function () {
show();
return orig.apply(this, arguments);
};
});
window.addEventListener('popstate', show);
window.addEventListener('hashchange', show);
})();
</script>