How to get image hight after the image has fully loaded? [duplicate]

问题: This question is an exact duplicate of: How to save image width and height from onload? 1 answer I have some images that are not small and I need t...

问题:

This question is an exact duplicate of:

I have some images that are not small and I need to use jQuery to get their height and width after they have loaded in the respective div. This question has been asked before and I tried many of the suggestions. For example: The following code was suggested and it only works intermittently on each page refresh:

jQuery(function() {
  jQuery(".youtube-image-thumbnail img").on('load', function() {
    console.log(jQuery(this).height());
  });

}); 

This solution did not work at all

jQuery(function() {
  console.log('here')
  jQuery(".youtube-image-thumbnail img").bind('load', function() {
    console.log(jQuery(this).height());
  });
});

My html element looks like this:

<div class="youtube-image-thumbnail">
<img src="/youtube_image/public/video_thumbnails/exvLChu1-fk.jpg?itok=q0nFDq8g" alt="" typeof="foaf:Image" class="img-responsive">
</div>

Here is a JsFiddle snippet Am I missing anything or is there a better more sound solution?


回答1:

At the time you've binded the img, it's already loaded.

This approach would work if you bind it first.

On (this example)[https://jsfiddle.net/oxdqe42L/4/] I've changed the img source after the bind, then the loading event has trigged the binded handler

  • 发表于 2018-12-27 20:25
  • 阅读 ( 233 )
  • 分类:网络文章

条评论

请先 登录 后评论
不写代码的码农
小编

篇文章

作家榜 »

  1. 小编 文章
返回顶部
部分文章转自于网络,若有侵权请联系我们删除