如何检查是否加载了背景图像? [英] How can I check if a background image is loaded?
本文介绍了如何检查是否加载了背景图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在body标签上设置背景图片,然后运行一些代码 - 如下所示:
I want to set a background image on the body tag, then run some code - like this:
$('body').css('background-image','http://picture.de/image.png').load(function() {
alert('Background image done loading');
// This doesn't work
});
如何确保背景图像已满载?
How can I make sure the background image is fully loaded?
推荐答案
试试这个:
$('<img/>').attr('src', 'http://picture.de/image.png').on('load', function() {
$(this).remove(); // prevent memory leaks as @benweet suggested
$('body').css('background-image', 'url(http://picture.de/image.png)');
});
这将在内存中创建新图像并使用load事件来检测src何时加载。
this will create new image in memory and use load event to detect when the src is loaded.
这篇关于如何检查是否加载了背景图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文