jQuery:预加载一组图像,但前提是没有其他内容加载

发布于 2024-08-19 23:17:12 字数 189 浏览 2 评论 0原文

是否可以使用 jQuery 来识别浏览器当前是否未加载任何内容,我的意思是加载图像和 .load Ajax 调用等。

我想要实现的目标是秘密(隐藏)预加载一组图像(重量较重)在后台运行,但不会分散其他加载进程的注意力。这意味着它们只会在浏览器没有其他事情可做的情况下预加载,并且在其他内容开始加载时也会停止预加载..

有什么建议吗?

Is it possible with jQuery to identify if the browser currently isn't loading anything, and with anything I mean both loading images and .load Ajax calls etc.

The thing I would like to achieve is to secretly (hidden) preload a set of images (heavy in weight) in the background, but without distracting the other load processes. Meaning they will only preload if the browser have nothing else to do, and also stop preloading if something else starts to load..

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷爱 2024-08-26 23:17:12

查看 Ajax 事件,特别是 ajaxStop 全局事件。至于了解图像何时完成加载, load可以使用事件。例如:

$("#myImage").load(function() {
    alert("image has completely loaded!");
});

将它们放在一起:

$("selector").ajaxStop(function(event,request,settings){
    var $img = $('<img src="foo.jpeg" id="myImage"/>');
    $img.load(function() {
        alert("image has completely loaded!");
        $("div").append($(this));
    });
});

Have a look at Ajax Events, in particular the ajaxStop global event. As for knowing when an image has finished loading, the load event can be used. For example:

$("#myImage").load(function() {
    alert("image has completely loaded!");
});

To put those together:

$("selector").ajaxStop(function(event,request,settings){
    var $img = $('<img src="foo.jpeg" id="myImage"/>');
    $img.load(function() {
        alert("image has completely loaded!");
        $("div").append($(this));
    });
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文