About Me

header ads

Display a loading icon until the page shows up

Display a loading icon until the page loads completely

 Poonam  CSSHow to 
Updated:
Isn’t it a great idea to show a cool animated image until your website is loading? Of Course, it will improve your site’s user experience.
Let’s learn how to use jQuery and CSS to show loading image while page loading. Free download loading gif images and use these 7 + 10 cool pre-loading icons.
Loading icon cubeLoading icon four squareLoading icon rubik's cubeLoading icon rectangle square dotsLoading icon tetris gameloading icon snake gameloading icon suffling rubik's cubeLoading icon rotating circlesloading icon fadein faceout circlesrotating two circles loaderLoading icon revolving circleshangout loader iconloading icon zoom in zoom out circlesLoading icon bouncing circleloading icon kinetic rotation circle crossgoogle like loaderloading icon conectric circles fading out
Free download loading gif images.:

How to show a Responsive loading icon or image while page loads.

1. Add a div just after <body> tag.
<div class="se-pre-con"></div>
2. Add some CSS to show the icon and bring it in the middle of the page.
/* Paste this css to your style sheet file or under head tag */
/* This only works with JavaScript, 
if it's not present, don't show loader */
.no-js #loader { display: none;  }
.js #loader { display: block; position: absolute; left: 100px; top: 0; }
.se-pre-con {
 position: fixed;
 left: 0px;
 top: 0px;
 width: 100%;
 height: 100%;
 z-index: 9999;
 background: url(http://smallenvelop.com/wp-content/uploads/2014/08/Preloader_11.gif) center no-repeat #fff;
}
3. Now add some jQuery to show the pre-loading icon when the page is loading and hide when it has finished loading.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script src="http://cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.js"></script>

//paste this code under the head tag or in a separate js file.
 // Wait for window load
 $(window).load(function() {
  // Animate loader off screen
  $(".se-pre-con").fadeOut("slow");;
 });
All done, now reload your page and it will show a loading icon.
Many of you have been searching for a way to show a loading gif while actual image is being loaded or show a spinner gif while an HTML element or div is being loaded. Let’s learn how to show a loading icon over each image on your site while the image is loading, using jQuery.

How to show loading gif while actual image is loading – Very simple method.

1. Add the following script to < head >.
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js'></script>
<script type='text/javascript' src='loadImg.js'></script>
<script type='text/javascript'>
    $(function(){
        $('img').imgPreload()
    })
</script>
The loadImg.js contains the plugin which will automatically detect the images on the page and hides it while the images are not loaded completely. Until then it will show a loading icon. You can change the ‘img’ to any div or class on which you want to show the spinner while it is loading.
The plugin loadImg.js contains the following code.
(function() {
  (function($) {
    return $.fn.imgPreload = function(options) {
      var delay_completion, i, image_stack, placeholder_stack, replace, settings, spinner_stack, src, x, _i, _len;
      settings = {
        fake_delay: 10,
        animation_duration: 1000,
        spinner_src: 'spinner.gif'
      };
      if (options) {
        $.extend(settings, options);
      }
      image_stack = [];
      placeholder_stack = [];
      spinner_stack = [];
      window.delay_completed = false;
      delay_completion = function() {
        var x, _i, _len, _results;
        window.delay_completed = true;
        _results = [];
        for (_i = 0, _len = image_stack.length; _i < _len; _i++) {
          x = image_stack[_i];
          _results.push($(x).attr('data-load-after-delay') === 'true' ? (replace(x), $(x).removeAttr('data-load-after-delay')) : void 0);
        }
        return _results;
      };
      setTimeout(delay_completion, settings.fake_delay);
      this.each(function() {
        var $image, $placeholder, $spinner_img, offset_left, offset_top;
        $image = $(this);
        offset_top = $image.offset().top;
        offset_left = $image.offset().left;
        $spinner_img = $('<img>');
        $placeholder = $('<img>').attr({
          src: 'data:image/gif;base64,R0lGODlhAQABA
                IABAP///wAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=='
        });
        $placeholder.attr({
          width: $image.attr('width')
        });
        $placeholder.attr({
          height: $image.attr('height')
        });
        spinner_stack.push($spinner_img);
        placeholder_stack.push($placeholder);
        image_stack.push($image.replaceWith($placeholder));
        $('body').append($spinner_img);
        $spinner_img.css({
          position: 'absolute'
        });
        $spinner_img.css('z-index', 9999);
        $spinner_img.load(function() {
          $(this).css({
            left: (offset_left + $placeholder.width() / 2) - $(this).width() / 2
          });
          return $(this).css({
            top: (offset_top + $placeholder.height() / 2) - $(this).height() / 2
          });
        });
        return $spinner_img.attr({
          src: settings.spinner_src
        });
      });
      i = 0;
      for (_i = 0, _len = image_stack.length; _i < _len; _i++) {
        x = image_stack[_i];
        x.attr({
          no: i++
        });
        src = x.attr('src');
        x.attr({
          src: ''
        });
        x.load(function() {
          if (window.delay_completed) {
            return replace(this);
          } else {
            return $(this).attr('data-load-after-delay', true);
          }
        });
        x.attr({
          src: src
        });
      }
      replace = function(image) {
        var $image, no_;
        $image = $(image);
        no_ = $image.attr('no');
        placeholder_stack[no_].replaceWith($image);
        spinner_stack[no_].fadeOut(settings.animation_duration / 2, function() {
          return $(this).remove();
        });
        return $image.fadeOut(0).fadeIn(settings.animation_duration);
      };
      return this;
    };
  })(jQuery);
}).call(this);
2. Add some markup having some images.
<div class="images">
 <ul>
  <li>
   <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/1.jpg">
  </li> 
  <li>
   <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/2.jpg">
  </li> 
 </ul>
</div>
3. Add some CSS code. Please make sure that the images have specific width and height so that the loading icon may be displayed in the center.
.images ul li img {
 width: 400px;
 height: 266px;
}
.images ul li {
 display: inline-block;
}
That’s all for the codes. You will able to show a loading icon over each image on the page while actual images are not fully loaded.
The final page would look like
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
<link rel="stylesheet" type="text/css" href="style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js"></script>
<script type='text/javascript' src='loadImg.js'></script>
<script>
$(function(){
        $('img').imgPreload()
    })
</script>
</head>
<body>
<p><a href="smallenvelop.com/display-loading-icon-page-loads-completely/"><--Back to the Article.</a></p>
<h1>Display a Loading icon while images loads</h1>

<div>
 <div class="images">
  <ul>
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/1.jpg">
   </li>
   
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/2.jpg">
   </li>
   
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/3.jpg">
   </li>
   
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/4.jpg">
   </li>
   
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/5.jpg">
   </li>
   
   <li>
    <img class="main-img" src="https://smallenvelop.com/demo/image-loading/images/6.jpg">
   </li>
  </ul>
 </div>
</div>


</body>
</html>
In the above demo, we have bundled 17 cool loading icons which you can use for your free and commercial project. If you want more icons just go to
This way we can show the loading image while your webpage is loading and while an image is completely loaded. I hope this simple preloader will help you improve your site’s user experience. If you get any difficulty using the codes to your website then do comment below. Feel free to contact me for any help.
Subscribe to our email list to get updates to this post.

Post a Comment

0 Comments