所以我有这个 html / css / jQuery / js 代码,它们一起显示了一个非常大的图像宽度。在加载和设置图像之前,基本元素应该是不可见的。然后通过一个小的淡入淡出使基础元素可见。
但是,行为有点不同。加载图像后,我可以看到小文本逐渐消失,并且需要几秒钟才能弹出一次图像(而不是像从上到下出现的加载样式)
这是我使用的简化代码:
<script>
$(function() {
$("#base").hide();
var baseHeight = $(window).height();
var backLayerSRC = $('#img').attr('data-src');
$('#base').height(baseHeight);
var img = new Image();
var imgWidth, imgHeight;
img.onload = function() {
imgHeight = this.height;
imgWidth = this.width;
var factor = 1 * baseHeight / imgHeight;
totalWidth = Math.round(factor * imgWidth);
currentWidth = totalWidth;
$('#base').width(totalWidth);
$('#img').attr('src', backLayerSRC);
$('#img').height(baseHeight);
$('#base').fadeIn(500);
};
img.src = backLayerSRC;
});
</script>
<style>
#base {
position:absolute;
left:0px;
height:100%;
}
#base #img {
position:absolute;
left:0px;
top:0px;
}
</style>
<div id='base'>
some tekst
<img src='' id='img' data-src='path_to_very_large/image.png' />
</div>
现在,它怎么可能不会随着图像淡入呢?
这是一个带有图像的示例,请检查以便清楚我的意思 http://jsfiddle.net/uz8mvtap/3