【问题标题】:Finish loading image before executing other code在执行其他代码之前完成加载图像
【发布时间】:2013-10-10 14:35:41
【问题描述】:

我正在使用模式窗口在缩略图上显示图片。
但是,调整大小的照片并未加载到模态窗口中。

图像的 src 最初不在我的标记中,但我通过动态附加将其添加到标记中。我第二次单击它,图片在模态中加载。

查看控制台时,我能够第一次获得高度和宽度。第二次我得到高度和宽度加上“图像已加载”文本。

我想检查照片是否已加载以及是否已完成,然后执行其余代码。如果没有加载,继续检查直到加载,然后继续执行其余代码。

我已阅读其他有关堆栈溢出的文章,但没有一篇对我有用。 有人可以帮忙吗。

 $(document).ready(function() {

    function galleryStart ($current) {


       var $tImage      = $current.find('img');
       var fullURL        = $tImage.attr('src');
       var $dFigure         = $('#dialog-media figure .media');
       var fullSrcURL = "images/mypicture"; 
       var fullSrcURL = fullURL.replace(".jpg", "_full.jpg"); 


       var image;

      function onload () {
        console.log(image.height, image.width);
        var objHeight = image.height;
      var objWidth = image.width;
      }

      image= new Image();
      image.src=fullSrcURL;

      if (image.complete) {
         console.log('image already loaded to browser');
         onload();
      } else {

         image.onload = onload;
         console.log('xxx');
      }

    $dFigure.append('<img id="dialog-media-object" src="' + fullSrcURL + '" alt="' +         $tImage.attr('alt') + '" />');

   //Execute more code here once the image has finished loading
   //More code to load the image into the modal window
}

$('a.dialog-media').click(function (event) {
    event.preventDefault();

            $("#dialog").dialog({
          height: "auto",  //720
          width: "auto",   //960
          draggable: false,
          modal: true,
          //position: {
          //          my: "center", 
          //          at: "center",
          //          of: window },
          open: function(event, ui){
              galleryStart(tLink);
          },
         close: function(event, ui){
              galleryEnd();
      $(this).dialog('destroy').remove();


});

}); // 最后的结束标签

【问题讨论】:

  • 在这 - $('#dialog-media figure .media'); figure 是什么?那应该是$('#dialog-media-figure .media'); 吗?
  • @Archer - figure 是一个新的 HTML5 标签。
  • 谢谢@Steve。不幸的是,我必须迎合 HTML5 之前的版本,所以还没有机会玩它。我很复古:(
  • @Archer - 我感觉到你的痛苦......我实际上不得不查找figure 以确认我没有记错。 :)

标签: jquery image caching onload


【解决方案1】:

您可以像这样对图像使用 onload 事件:

image= new Image();
image.onload = onLoadHandler;
image.src=fullSrcURL;

function onLoadHandler() {
    alert("Image is loaded!");
}

注意:在分配源之前,请务必将 onLoadHandler 分配给 image.onload

所以在你的情况下,我会跳过整个 image.complete 语句并执行以下操作:

var $dFigure = $('#dialog-media figure .media');
var fullSrcURL = "images/mypicture"; 
var image;

function onload () {
  console.log(image.height, image.width);
  var objHeight = image.height;
  var objWidth = image.width;
  $dFigure.append('<img id="dialog-media-object" src="' + fullSrcURL + '" alt="' + $tImage.attr('alt') + '" />');
}

image= new Image();
image.onload = onload;
image.src=fullSrcURL;

【讨论】:

  • 我是否将其余代码放在函数 onLoadHandler 中,以便在加载图像后执行其余代码?
  • 如果您的代码的执行取决于图像的可用性,那么是的,您必须将该代码放在onload 函数中(或至少从那里触发它)。跨度>
  • 我会试试这个并告诉你结果。
【解决方案2】:

用 jQuery 简单地创建 DOM 元素:

(function($){
    $(function () {
        // create image
        $('<img>')
            // add onLoad handler
            .load(whenImageIsLoadedFunction)
            // set the images 'src'
            .attr('src', 'your/image/source.png');
    });

    function whenImageIsLoadedFunction() {
        // do all your code here.
        $(this).appendTo('#imageContainer');
    }
}(jQuery));

【讨论】:

  • 我并不反对这个答案,但是对于这么简单的事情,jQuery 函数的开销如此之大,以至于我更喜欢​​纯 JS 方法。
  • 好吧,如果我们谈论的是数千张图片,您可能会说得有道理。但即使我们愿意,DOM API 也会在 JS 注意到过载之前说再见。顺便说一下,您的解决方案会创建 2 个图像实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
  • 2017-10-27
  • 2013-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多