【问题标题】:JQuery load event on images图像上的 JQuery 加载事件
【发布时间】:2012-07-31 21:50:32
【问题描述】:

我想在加载图像时将图像父级的大小调整为与图像相同的大小。

此时我正在使用此代码:

$(window).load(function(){
    $('.image-principale').each(function(){
        $(this).parent().css('height', $(this).height());
    });
});

它工作,除了它只在 每个 图像加载时运行。 我尝试直接为每个图像添加一个负载处理程序,但它们没有触发。

怎么了?

谢谢!

【问题讨论】:

  • 您希望它在每个图像加载时运行?
  • 是的,每个图像都有一个特定的类

标签: jquery image load handler


【解决方案1】:

尝试以下方法:

...Your HTML...

<script type="text/javascript">
    $('.image-principale').load(function(){
        $(this).parent().css('height', $(this).height());
    });
</script>

请注意,脚本必须放在 HTML 之后,否则它将在 &lt;img&gt; 元素存在之前运行,并且实际上不会执行任何操作。

您也可以尝试使用live,如下所示:

$('.image-principale').live('load', function(){
    $(this).parent().css('height', $(this).height());
});

但是,我不知道它是否真的有效。

【讨论】:

  • 你说的很对:在这种情况下将处理程序放在 ready() 中没有意义,所以你应该把 JS 放在 HTML 之后,所以我 +1。另外,点击投票的东西,你目前有 1 上,0 下。没有反对意见。
  • 有,但被删除了。
  • 对不起,它不起作用。我想在文档准备好之后但在窗口加载之前将加载事件绑定到每个匹配的元素。
  • 为什么在文件准备好之后需要它?把它放在 HTML 之后应该就足够了。
  • 来自 jQuery 文档。事件就绪:指定 DOM 完全加载时执行的函数。
【解决方案2】:

准备好文件很容易:

$(function() {
  $('.image-principale').load(function(){
    $(this).parent().css('height', $(this).height());
  });
});

甚至:

$(function() {
  $('.image-principale').live('load', function(){
    $(this).parent().css('height', $(this).height());
  });
});

外部或内部ready()

【讨论】:

    【解决方案3】:

    基本上,您想监听来自 img 元素的 load 或 readystatechange 事件。

    所以你需要这样做:

    $(window).load(function(){
        $('.image-principale img').on("load readystatechange", function(){
            // Here you can check whether it state complete by checking 
            // if(this.complete || (this.readyState == 'complete' && e.type == 'readystatechange') ) { }
            $(this).parent().css('height', $(this).height());
        });
    });
    

    但是...这种方式有一些(大)警告。即它不会“总是”工作,它不会与“缓存”图像一起工作(有时)。 最好的方法实际上是从 Desandro,一个名为 jquery.imagesLoaded

    它将检查加载事件和其他一些条件(如损坏的图像、缓存的图像、加载的元素……),并在加载所有元素时为您提供反馈。

    用法是(你可以在网站上看到更多):

    // Loading script 
    var dfd = $('#my-container').imagesLoaded(function($images, $proper, $broken){
        //Here you can do a loop on $proper that is the list of images that properly loaded.
    }); // save a deferred object and use dfd later
    

    您还可以为每个加载的图像设置一个回调:

    dfd.progress(function( isBroken, $images, $proper, $broken ){ });
    

    我很确定这会解决您的(非常老的……抱歉)问题。我在这里发布它是因为它可以帮助其他人。

    【讨论】:

      【解决方案4】:

      为我工作:http://jsbin.com/ululo/edit

      $(function(){
        $("img").load(function(){
          $(this).parent().height( $(this).height() ); 
        }); 
      });
      

      【讨论】:

      • 那是因为JSBin在HTML之后发出脚本;他将脚本放在 HTML 之前。看我的回答。
      • 这就是我需要的,但它不适用于一张以上的图片
      • @Jonathan:在这里不起作用.. jsbin 上的第 5 版。我正在使用 FF 3.6
      • @Hubert:我刚刚在 FF 3.5.7 中尝试过,效果很好。逐步加载每个图像,然后将其包装器的大小调整为它们的高度。
      • @Jonathan:哦,我通过清除缓存让它工作了。实际上它有效,但只是第一次。当图像被缓存时,加载事件不会触发。有什么想法吗?
      【解决方案5】:

      $('img').load 似乎每次调整图像大小时都会触发。就我而言,它使我的网站变慢。 (我在FF上试过)

      【讨论】:

        猜你喜欢
        • 2010-10-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-03-09
        • 2011-08-28
        • 2013-07-07
        相关资源
        最近更新 更多