I am struggling with a little issue with a practice project I am working on.
I’ve made a header with a few social media icons on the right side of it. Those icons are in black and white but the moment you hover over it they a colored version takes its place. This part all works fine. But the thing, the first time after loading the webpage and you hover over those icons, you see a little flickering. Because at that moment the image is loading in. So I want to pre-load those colored images the moment you load the site to prevent this. I found a “solution” online but that didn’t seem to work, at least not for me, maybe I do something wrong. Well this is what I basically did:
So this should make sure the images are pre-loaded and will stay hidden until you hover over the icons. Though yeah, somehow it doesn’t seem to work for me.
After that I also tried some other way that I found online. Trying to pre-load the images in within the head of the html document like this, but that also didn’t seem to have any effect:
i did a little of my own research just now and someone suggests that you load the image with a img element instead of as a div and then add your hover effect on the img element directly.
I think it is worth a try.
yes it is good practice to assign all images a width and height to prevent flickering.
But I assumed the issue was not with the image when there was no hover effect (I assumed the hover effect was triggering the issue).
Thanks for all your suggestions guys. Very much appreciate it.
I combined to solutions of @hbar1st and @lasjorg using direct img elements and opacity.
I will show you what I did now. Not sure if this is the most “efficient code” maybe there is a better or shorter way to do it, but at least it seems to work fine now without any flickering after first loading the site haha.
Well, I’d love to hear what you guys think of this solution. Is this the correct way to put it or is there another maybe a more efficiënt way to do it? Love to learn.
And thanks again for the assistance. Very much appreciated!
I had never heard of the opacity trick before.
I guess the only way to know if something is efficient or not is to undo the change to load both images and just load one and see what happens. I am guessing though that loading both is definitely faster on the response time wrt the hover effect.