简体   繁体   English

在完全加载时将事件分配给div

[英]assign event to div when it has loaded fully

Can I assign an event to a div to fire when all elements of the containing div have loaded fully? 当包含div的所有元素都已完全加载时,我可以将事件分配给div来触发吗? eg. 例如。 if the div contains an image, fire an event on the div when the image has loaded fully. 如果div包含图像,则在图像完全加载时在div上触发事件。 I am using jquery. 我正在使用jquery。

Not sure how you're dynamically adding your content, but this example should illustrate how it could work. 不确定您是如何动态添加内容的,但此示例应说明它是如何工作的。

It is using .append() to simulate your dynamically loaded content. 它使用.append()来模拟动态加载的内容。 Then after the append, it uses .find() to find the images, and .load() to attach a load handler to them. 然后追加后,它使用.find()找到图像, .load()的负载处理程序附加给他们。

Finally, it returns the length property of the images. 最后,它返回图像的length属性。 Then in the load handler, as the images finish loading, the length is decremented. 然后在加载处理程序中,当图像完成加载时,长度递减。 Once it gets to 0 , all the images are loaded, and the code inside the if() runs. 一旦它变为0 ,所有图像都被加载,并且if()内的代码运行。

Example: http://jsfiddle.net/ehwyF/ 示例: http //jsfiddle.net/ehwyF/

var len = $('#mydiv').append('<img src = "http://dummyimage.com/150x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/160x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/170x90/f00/fff.png&text=my+image" /><img src = "http://dummyimage.com/180x90/f00/fff.png&text=my+image" />')
    .find('img')
    .load(function() {
        if( --len === 0) {
            alert('all are loaded');
        }
    }).length;

This way, the code runs based only on the images in #mydiv . 这样,代码仅基于#mydiv的图像#mydiv

If there are any images in there that were not dynamic, and therefore shouldn't get the .load() event, you should be able to add a .not() filter to exclude ones the complete property is true . 如果那里的任何图像不是动态的,因此不应该得到.load()事件,你应该能够添加一个.not()过滤器来排除complete属性为true

Place this after the .find() and before the .load() . 将这个后.find()和前.load()

.not(function(){return this.complete})

It's hard to check it for just one div, but may I suggest using $(window).load() to check if all the images have loaded? 很难检查它只有一个div,但我可以建议使用$(window).load()来检查所有图像是否已加载?

UPDATE: If you using ajax then use the callback to see when the json has loaded, and then attach $("div#id img").load() to see when all the images have loaded. 更新:如果您使用ajax然后使用回调查看json何时加载,然后附加$(“div#id img”)。load()以查看所有图像何时加载。 Only problem with this is that I have noticed when the image is cached, the loader does not get triggered. 唯一的问题是我注意到图像被缓存时,加载器不会被触发。 :( :(

如果它只是一个你想要等待的图像,你可以用ajax加载图像,然后在ajax回调中触发事件。

To add to Shadow Wizard's idea, you could try the following code: 要添加Shadow Wizard的想法,您可以尝试以下代码:

var totalImages = $(".yourImageClass").length;
var loadedImages = 0;
$(".yourImageClass").bind("onload", function() {
    loadedImages++;
    if (loadedImages == totalImages) {
        //all images have loaded - bind event to DIV now
        $("#yourDiv").bind(eventType, function() { 
            //code to attach 
        });
    }
});

Catch the onload event of all images in the div, in there raise some global counter by 1. If that counter is equal to the amount of images in the div... all images finished loading. 捕获div中所有图像的onload事件,在那里将一些全局计数器提高1.如果该计数器等于div中的图像数量...所有图像都完成加载。 Should be simple using jQuery. 使用jQuery应该很简单。

This will work only for images though, other elements don't have onload event. 这仅适用于图像,但其他元素没有onload事件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM