Fork me on GitHub

imagesLoaded 监听 background-image 加载失败

imagesloaded文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<div class="js-cover-loaded" data-cover="静态图" style="background-image:url(动态图)"></div>
// 加载动图失败时尝试加载静态图
var imgLoad = imagesLoaded(".js-cover-loaded", { background: true });
imgLoad.on("always", function(){
for ( var i = 0, len = imgLoad.images.length; i < len; i++ ) {
var image = imgLoad.images[i];
var $ele = $(imgLoad.elements[i]);
$ele.removeClass("js-cover-loaded");
if(!image.isLoaded){
$ele.css({
backgroundImage : "url(" + $ele.data("cover") + ")",
});
}
}
});
-------------感谢您的阅读 有问题请留言(或mailto:frostbelt@sina.cn)-------------