preload image and show a spinner while loading

自闭症网瘾萝莉.ら 提交于 2019-12-06 13:39:09

You can somehow harness the load event, e.g.:

$(".thumbs li a").click(function(){
    var largePath = $(this).attr("href");
    $('.thumbs li').removeClass('thumbac');
    $(this).parent().addClass('thumbac');
    $("#largeImg").hide()
                  .attr({ src: largePath })
                  .load(function() {
                       $(this).show();
                   });
    return false;
});

here is how you can preload images in jq:

    (function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

and then you preload them like this:

jQuery.preLoadImages("image1.gif", "/path/to/image2.png");

You can use my plugin imgPreload which while preloading an image displays a nice spinner.

In your case you would just need to add one line to your existing code:

$("#largeImg").attr({ src: largePath }); //your code
$("#largeImg").imgPreload() //the extra line

Please see this page http://denysonique.github.com/imgPreload/ for a demo and documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!