Show loading indicator with jQuery

后端 未结 4 471
时光说笑
时光说笑 2021-01-07 00:54

I have a

with an image slider in an
  • . There are 12 images, and it takes some time to load all of them. While the images load,
  • 相关标签:
    4条回答
    • 2021-01-07 01:03

      You can use the jQuery load() function. It will run the code once the image has loaded. So if you have a gif shown, once the image has loaded, it will hide it.

      Javascript

      $('img#id').load(function(){
          $('#loadingGif').hide();
      });
      

      HTML

      <div id="loadingGif">
          <img src="loading.gif" />
      </div>
      
      0 讨论(0)
    • 2021-01-07 01:10

      You can bind a listener to the load event of each image. Use a counter or some other mechanism to keep track of how many images are loaded each time the listener is invoked. Then hide the loading indicator after the last image is loaded. For example:

      HTML

      <span id="loading" style="display:none">Loading...</span>
      <div class="images">
          <img class="large-image" src="http://astritademi.files.wordpress.com/2011/04/happy_panda.jpg" width="893" height="548" />
          <img class="large-image" src="http://www.pandafix.com/pandafix/images/r3387761054.jpg" width="275" height="199" />
          <img class="large-image" src="http://farm1.static.flickr.com/149/357563212_f8b89ac6d8.jpg" width="500" height="375" />
      </div>
      

      jQuery

      $(function() {
          var images = $('.large-image')
            , total = images.length
            , count = 0;
      
          $('#loading').show();
          images.load(function() {
              count = count + 1;
              if (count >= total) {
                  $('#loading').hide();
              }
          });
      });
      

      You can see this example in action on jsFiddle: http://jsfiddle.net/hallettj/ZefaM/

      0 讨论(0)
    • 2021-01-07 01:23

      This is what you need: http://jqueryfordesigners.com/image-loading/

      0 讨论(0)
    • 2021-01-07 01:26

      if u loading all images via AJAX then use below way

      HTML

      <div id="loadingDiv"><img src="loading.gif"></div>
      

      jQuery

      $('#loadingDiv')
          .hide()  // hide it initially
          .ajaxStart(function() {
              $(this).show();
          })
          .ajaxStop(function() {
              $(this).hide();
          })
      ;
      
      0 讨论(0)
    提交回复
    热议问题