有没有办法使用 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();
});
关于jquery - 背景图像加载时触发事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3281651/