加载图标作为背景图像加载

加载背景图像需要一些时间。

如何检查背景图像(使用background-image CSS标签应用)是否已完成加载?

默认情况下,您可以将加载图标包含为默认背景图像,然后在下载缓慢加载的图像后通过javascript更新CSS。

这是一个显示该技术的工作示例:

$(document).ready(function() { var imgSrc = 'http://sofzh.miximages.com/jquery/2801924702_ffbdeda927_d.jpg'; var img = new Image(); img.onload = function() { $("div").css("background-image", "url('" + img.src + "')"); }; img.src = imgSrc; }); 
 div { background-color: orange; padding: 50px; color: purple; font-weight: bold; } 
  
.

This div will have a solid color as background to begin with.

Watch its background change to an image once it's loaded.