Display a "Loading" image in the gallery before anything else loads

Can a animated loading gif be implemented to load before the gallery images, or would it serve no purpose?

The image will be loaded as a background using CSS.

<link rel="stylesheet" href="loading.gif" />

Appreciate your help!

Answer №1

Start by loading the "spinner" image first. Once the gallery images have finished asynchronously loading, swap out the spinner with the newly loaded images.

Answer №2

My preferred method is to provide a reference link instead of duplicating code, and here is an example from CSS tricks.

Answer №3

If you want to hide an image and then switch it in when it loads, you can achieve this by placing a spinner behind the image and using some CSS trickery. Wrap the image in a div element where you define your spinner, then use CSS to hide the image. Trigger a function on the image's onload event to fade the image in once it has loaded.

Here is a simple example of how to accomplish this:

http://jsfiddle.net/4a1wj359/1/

// Function to display the image when it loads
var loaded = function(img){
   img.classList.add('loaded');
};

  <div class="img_wrapper">
    <img id="image1" src="http://theselby.com/media/2_28_11_SupremeCoffee7598.jpg" alt="" onload="loaded(this)"/>
 </div>

Check out the fiddle for the complete CSS code. In my example, I use a black box as the background instead of a spinner. You can replace the black box with your own spinner image. Additionally, I add a fading effect to the image once it finishes loading, but you can customize this according to your preferences.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

I'm struggling to understand the folder arrangement and the system is telling me it can't locate my file

I'm having trouble identifying what might be causing issues with my file structure. Currently, I am using Aurelia for the front end and node for the server. I attempted a fix by performing a join operation, which resolved some of the problems. However ...

Sending AJAX Responses as Properties to Child Component

Currently, I am working on building a blog using React. In my main ReactBlog Component, I am making an AJAX call to a node server to get back an array of posts. My goal is to pass this post data as props to different components. One specific component I h ...

Bring in a JavaScript file from Blogger without using a tag

Is there a way to retrieve blogger feeds code without using