我有几张超级英雄的图像。这些都与天文物体的更大的图像一起呈现。这些较大的图像需要一段时间才能加载。我希望天文图像在加载后替换超级英雄图像。用较大的图像替换较小的图像(在较大图像加载后)
这是我到目前为止有:https://jsfiddle.net/vmpfc1/5typ7pnp/1/
HTML:
<body>
<div class="image-wrapper">
<div class="pix"style="background: url('http://baltimorepostexaminer.com/wp-content/uploads/2012/07/spider-man2_pole_4681.jpg'); height:200px;width:200px;">
</div>
<div class="true-image" style="background: url('http://m1.i.pbase.com/o9/27/876727/1/151851961.JlvdQ9xW.GreatCarinaKeyholeandRedHoodNebulae3454x2566pixelsimproved3.jpg')"></div>
</div>
<div class="image-wrapper">
<div class="pix"style="background: url('https://upload.wikimedia.org/wikipedia/en/7/75/Comic_Art_-_Batman_by_Jim_Lee_%282002%29.png'); height:200px;width:200px;">
</div>
<div class="true-image" style="background:url(' https://www.nasa.gov/sites/default/files/706439main_20121113_m6triptych_0.jpg')"></div>
</div>
</body>
JS:
setTimeout(function() {
$('.true-image').attr('style').on('load', function() {
$('.pix')({
'background-image': 'url(' + $(this).attr('src') + ')'
});
});
}, 0);
CSS:
.true-image {
display : none;
}
我是一个JavaScript新手 - 有一个十二月如何让更大的空间图像取代超级英雄占位符?
在HTML5中有更简单的方法吗?
延迟加载 - https://css-tricks.com/snippets/javascript/lazy-loading-images/ – CodeRomeos