I am facing a bit of an issue and cant seem to find a solution. I have a section tag
<section class="background-gif"></section>
This simply loads a background image
.background-gif {
background: url(../images/image.gif) no-repeat top;
background-size: 100%;
}
Straightforward enough. Problem is, the gif that is being loaded is 5MB as it has a lot of animation. This is causing the page load to be mega slow. I can't use a standard preloader, to do with requirements.
Instead, I thought I would give something like this a go https://medium.com/front-end-hacking/how-to-optimize-image-loading-on-your-website-855020fb41ae
However, my IDE does not seem to like this code, I think it is ES6? So I essentially trying to do something similar. My thought is to replace the above CSS so it initially displays a static image. And then in the background, the gif can load, and once loaded, replace the static image.
I have seen examples where an Image Object is used, something like this Dynamically replace image src after the page loaded and the image is completely downloaded
I cant however find anything that does this with background images.
How would I go about replacing the static background once the main gif has fully loaded?
Thanks
By giving the
section.background-gifa placeholder image(in your case it can be a minified image from theGIFimage that you want to load) , and give it adata-srcattribute containing the path/link of the desiredGIFimage, then usingJavaScriptwe'll load theGIFimage based on thedata-srcattribute of thesection.background-gifelement, and when it loads we'll assign itssrcattribute to thebackground-imageproperty of thesection.background-gifelement.Here's a runnable snippet to illustrate:
Hope I pushed you further.