我知道如何延迟加载元素,但我有一个来自客户的奇怪请求。他们希望背景图像在页面加载后 5 秒加载。我是 js 的新手,以为我可以轻松做到,但没有成功。
我已经试过了:
$(document).ready(function() {
$('#primary .bcg').css( "background", "url(folder/path", function(){
$(this).delay(5000).fadeIn(400)
});
});
还有这个:
$(document).ready(function() {
$('#primary .bcg').delay(5000).fadeIn(400, function() {
$('this').css( "background", "url(folder/path")
});
});
两者都无济于事。有没有办法做到这一点?或者更确切地说,是一种让新手实现它的方法?
CSS:
#primary .bcg {
background: none;
background-attachment: fixed;
background-position: center center;
background-repeat: no-repeat;
background-size: cover;
}
非常感谢您的帮助!
最佳答案
你可以像这样使用 setTimeout as"
$(document).ready(function(){
setTimeout(function(){
$('#Primary .bcg').css('background', 'url(folder/path)');
}, 5000); // Delay for 5 seconds
});
关于jquery - 如何延迟加载背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25658019/