到目前为止,这是我的解决方案(但我得到的是黑色背景,而不是图像)。
代码:
<script>
// Preload big parallax image
function preloadImage() {
return new Promise(function(resolve, reject) {
var img = new Image();
var img_url = 'assets/images/masthead.jpeg';
img.src = img_url;
resolve(img_url);
});
}
$(document).ready(function(){
preloadImage().then(function(fromResolve){
$('.ui.inverted.segment.masthead').css('background-image', 'url(' + fromResolve + ')');
});
});
</script>
感谢您的帮助。
答案 0 :(得分:0)
您可以尝试以下方法:
// Preload big parallax image
function preloadImage() {
return new Promise(function(resolve, reject) {
var img = new Image();
var img_url = 'assets/images/masthead.jpeg';
img.onload = function(x){resolve(img_url);};
img.onerror = function(x){reject([err,img_url]);};
img.src = img_url;
});
}
preloadImage()//load the images first
.then(function(fromResolve){
$(document).ready(//wait for document to be ready
function(){
$('.ui.inverted.segment.masthead').css('background-image', 'url(' + fromResolve + ')');
}
)
})
.catch(//if something went wrong
function(info){
console.log("image load failed because:",info[0],"url:",info[1])
}
);