如何判断图像是否在JQuery中加载或缓存? [英] How to tell if an image is loaded or cached in JQuery?

查看:86
本文介绍了如何判断图像是否在JQuery中加载或缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果以前已缓存图像,似乎.load()函数不会触发.因此,如果您要确保在加载一幅图像之前先加载一幅图像并显示另一幅图像(例如,放大镜),则无法执行以下操作:

Seems the .load() function doesn't fire if the image has been cached before. So if you want to make sure one images has loaded before you load and show another (i.e. Magnifier) you can't do something like:

$(img_to_load_first)
    .load(
        $(img_to_load_last)
            .src("2nd.png");
    )
    .src("1st.png");

那么如何确保JQuery中的加载顺序?

So how does one ensure loading order in JQuery?

推荐答案

您需要做的是有选择地处理您的负载绑定.您可以通过测试Image对象属性complete来验证负载.

What you'll have to do is handle your load bindings selectively. You can verify load by testing the Image object property complete.

例如:

$('.load_selector').each(function(){

    if (!this.complete) {
        $(this).load(function(){
            // handle image load event binding here
        });
    } else {
        // handle image already loaded case
    }

});

请注意,上面的this(单独使用)是指对jQuery提供的图像对象的DOM引用.

Note that above, this (used alone) refers to the DOM reference to the image object provided by jQuery.

这篇关于如何判断图像是否在JQuery中加载或缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆