简体   繁体   English

我怎么知道 <img> 使用jQuery在div中加载

[英]How can i know if all <img> loaded in div using jQuery

How can i know if all loaded in div using jQuery 我怎么知道是否全部使用jQuery加载到div中

i want to do this after load all img in #slider div 我想在#slider div中加载所有img后执行此操作

var imgHeight = $("#slider img").height();
alert(imgHeight);

You can use the load event 您可以使用load事件

$('#slider img').load(function(){
    var imgHeight = $(this).height();
    alert(imgHeight);
});

if there are more than one image, and you only want to obtain the height after they have all loaded, try this code 如果有多个图像,并且只想在它们全部加载后获得高度,请尝试以下代码

var img = $('#slider img');
var length = img.length;

img.load(function(){
    length--;

    if(length === 0){
        alert("All images loaded");
    };
});

Well, I've tested the code, and it appears that the problem hasn't got anything to do with the code. 好吧,我已经测试了代码,看来问题与代码无关。 When loading the page with the images already in the cache, this is what I get: 当将页面中已包含图像的图像加载到缓存中时,这是我得到的:

替代文字

Strangely, this does not occur when I force the browser not to use the cache. 奇怪的是,当我强制浏览器不使用缓存时,不会发生这种情况。

Try this: 尝试这个:

  • Attach an onload event listener to each image in the slider. onload事件侦听器附加到滑块中的每个图像。
  • In the listener: 在侦听器中:
    • Give the current image a custom attribute to mark it is 'ready'. 给当前图像一个自定义属性,以将其标记为“就绪”。
    • Check if all images are ready. 检查所有图像是否准备就绪。 If so, do your thing (ie alert(imageHeight) ) 如果是这样,请执行您的操作(即alert(imageHeight)

untested: 未经测试:

(function(){
  var slider=document.getElementById('slider'),
      images=slider.getElementsByTagName('img'), image, i=-1;

  while (image=images[i]) {
    image.onload=function(){
      this['data-ready']=true;
      var image, i=-1;
      while (image=images[i]) if (!image['data-ready']) return;
      // all images are ready; do your thing here
      // ...
    }
  }
}());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery加载页面后,如何淡入div? - How can I fade a div after the page is loaded using jQuery? 我已成功将表单加载到<div>使用 jQuery $.Ajax 的元素。 我需要知道如何使用 jQuery Ajax 提交该表单 - I have successfully loaded a form into a <div> element using jQuery $.Ajax. I need to know how to submit that form using jQuery Ajax 如何在特定div中使用jquery向img src添加其他路径? - How I can add additional path to img src using jquery in a certain div? 如何使用javascript / JQuery在所有dom图像上交换img src中的一个术语? - How can i swap one term in img src on all dom images using javascript / JQuery? 使用yepnope,如何知道CSS资源是否已成功加载? - Using yepnope, how can I know if a css resource was loaded successfully? jQuery UI:我怎么知道div集中在拖动事件上 - jQuery UI : How can I know that the div are center on drag event 使用JavaScript或jQuery,如何在<img>或<div>元素中专门移动鼠标时获取RGB颜色 - Using JavaScript or jQuery, how can I get the RGB color where ever the mouse is moving specially in <img> or <div> elements 如何知道何时加载特定“div”中的所有图像? - How to know when all images inside a specific “div” are loaded? 如何判断何时使用jQuery加载页面上的所有图像? - How can I tell when all images on a page are loaded using jQuery? 使用jQuery,如何检查所有图片是否都已装入动态dom结构中? - Using jQuery, how can I check when all images have loaded within a dynamic dom structure?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM