jquery – 背景图像加载时的触发事件

前端之家收集整理的这篇文章主要介绍了jquery – 背景图像加载时的触发事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法使用jQuery来触发CSS背景图像的加载事件?

解决方法

你可以这样做,

demo

$('<img>').load(function(){
    // make sure to bind the load event BEFORE setting the "src"
    alert('image loaded');
}).attr('src',function(){
    var imgUrl = $('body').css('background-image');
    imgUrl = imgUrl .substring(4,imgUrl .length-1);
    return imgUrl;
}).each(function() {
    // fail-safe for cached images which sometimes don't trigger "load" events
    if(this.complete) $(this).load();
});
原文链接:https://www.f2er.com/jquery/179712.html

猜你在找的jQuery相关文章