如何使用jQuery异步加载图像,如何知道图像加载何时完成?

时间:2009-12-19 15:15:08

标签: javascript jquery ajax

如何使用jQuery异步加载图像,以及如何知道图像加载何时完成?

3 个答案:

答案 0 :(得分:3)

How to check that image is loaded。它使用jquery ajax load()函数。

答案 1 :(得分:2)

JQuery Image Loading

  

在我们的基本版本中,我们将有一个包含加载微调器的div,一旦加载了大图像(在后台),我们将删除微调器并插入我们的图像。

您知道图像何时成功加载,当exectution达到success选项中指向的功能时,例如:

$.ajax({
  url: 'image-map.php',
  data: 'img=' + i.src, // the image url links up in our fake database
  dataType: 'html',
  success: function (html) {
    // because we're inside of the success function, we must refer
    // to the image as 'img' (defined originally), rather than 'this'
    $('#loader')
      .removeClass('loading')
      .append(img)
      .append(html);

    // now show the image
    $(img).fadeIn();
  }
});

答案 2 :(得分:2)

jQuery有一个加载方法,可用于在下载图像时通知您。因此,在您的情况下,您将使用jquery发出一个ajax请求,该请求将src返回到图像,然后创建一个新的图像对象并应用如下所示的加载方法。

var $img = $('<img>');
$img.attr('src', 'image.jpg').load(function() {
   alert('Image Loaded');
   //use jquery here to insert your image into the DOM
});

我希望这很清楚。如果您询问图像加载,我认为您知道如何制作ajax请求。

由于 尼克