CSS 背景图像完全加载时的 JQuery 绑定事件

发布于 2024-12-12 08:36:55 字数 249 浏览 5 评论 0原文

我想在背景图像完全加载后触发一个事件。 该图像是由动态 PHP 脚本制作的。

$("#box").css("background-image","url(image.php)");

也许我可以尝试在不可见的 中获取图像,并在图像加载时 (.load()) 使用不可见的 的 src 设置背景图像?不确定...

谢谢你的帮助。

I would like to trigger an event after a background-image is FULLY loaded.
The image is made from a dynamic PHP script.

$("#box").css("background-image","url(image.php)");

maybe I can try to get the image in a invisible <img> and when the image load (.load())
set the background-image with the src of that invisible <img> ? not sure...

thank you for your help.

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

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

发布评论

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

评论(2

南七夏 2024-12-19 08:36:55

您可以使用我的名为 waitForImages 的 jQuery 插件,这将有助于解决此问题...

$("#box").css("background-image", "url(image.php)").waitForImages({
   waitForAll: true,
   finished: function() {
       // Background image has loaded.
   }
});

否则,请手动执行此操作。 ..

var image = 'image.php',

    img = $('<img />');

img.bind('load', function() {
     // Background image has loaded.
});

img.attr('src', image);

$('#box').css('background-image', 'url(' + image + ')');

You could use my jQuery plugin called waitForImages that would help with this...

$("#box").css("background-image", "url(image.php)").waitForImages({
   waitForAll: true,
   finished: function() {
       // Background image has loaded.
   }
});

Otherwise, to do it manually...

var image = 'image.php',

    img = $('<img />');

img.bind('load', function() {
     // Background image has loaded.
});

img.attr('src', image);

$('#box').css('background-image', 'url(' + image + ')');
メ斷腸人バ 2024-12-19 08:36:55
$("#box img").load(function() {  
    //the image elements in #box are fully loaded.
});
$("#box img").load(function() {  
    //the image elements in #box are fully loaded.
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文