How to display a pending gif until the image is fully loaded

Most popular browsers, when rendering an image, display it line by line from top to bottom when it loads.

I have a requirement that the wait gid be displayed during image loading. When the image is fully loaded, it should be displayed instead of the pending gif.

+4
source share
2 answers

You can use jQuery load method

You can look here:

http://jqueryfordesigners.com/image-loading/

This is one implementation of the solution.

+5
source

A clean javascript solution is the following:

 var realImage = document.getElementById('realImageId'); var loadingImage = document.getElementById('loadingImage'); loadingImage.style.display = 'inline'; realImage.style.display = 'none'; // Create new image var imgPreloader = new Image(); // define onload (= image loaded) imgPreloader.onload = function () { realImage.src = imgPreloader.src; realImage.style.display = 'inline'; loadingImage.style.display = 'none'; }; // set image source imgPreloader.src = 'path/to/imagefile'; 
+5
source

Source: https://habr.com/ru/post/1304853/


All Articles