【发布时间】:2014-02-12 01:49:33
【问题描述】:
我有一个棘手的小问题。我刚刚开始设计一个带有自行车背景图像的网站。目前它工作正常,尽管您可以看到备用背景图像与第一个看起来有点混乱的图像一起加载。我可以很容易地隐藏这些图像,但我觉得更好的解决方案是让第一个图像在其他图像之前加载以加快页面加载速度。不幸的是,我无法弄清楚如何将其合并到我现有的 JS 中。
网站的开头是here(它可能会在某个时候被破坏,我仍在试验中)。
我现在的 JS:
function cycleImages(){
var $active = $('#background-cycler .active');
var $next = ($('#background-cycler .active').next().length > 0) ? $('#background-cycler .active').next() : $('#background-cycler img:first');
$next.css('z-index',2);//move the next image up the pile
$active.fadeOut(1500,function(){//fade out the top image
$active.css('z-index',1).show().removeClass('active');//reset the z-index and unhide the image
$next.css('z-index',3).addClass('active');//make the next image the top one
});
}
$(window).load(function(){
$('#background-cycler').fadeIn(1500);//fade the background back in once all the images are loaded
// run every 7s
setInterval('cycleImages()', 7000);
})
HTML:
<div id="background-cycler" >
<script type="text/javascript">
$('#background_cycler').hide();//hide the background while the images load, ready to fade in later
</script>
<img class="active" src="images/bg1.jpg" alt=""/>
<img src="images/bg2.jpg" alt=""/>
<img src="images/bg3.jpg" alt=""/>
<img src="images/bg4.jpg" alt=""/>
<img src="images/bg5.jpg" alt=""/>
</div>
您能给予的任何帮助将不胜感激!! :)
编辑:我遇到的另一个问题是我在每个页面上都需要这个,但我不想每次都重新加载图像。我是否认为图像仍会被缓存?
【问题讨论】:
标签: javascript jquery image background cycle