2015-10-07 79 views
0

我使用下面的代码来更改我的html页面的背景图像,但是我只想要第一个图像显示一次,其他图像保持旋转状态。寻求帮助,我应该换什么/添加到该代码要做到这一点使用Javascript更改背景图像,只显示第一个图像一次

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script> 
$(document).ready(function() { 
    var header = $('body'); 

    var backgrounds = new Array(
     'url(http://www.example.com/image-1.png)', 'url(http://www.example.com/image-2.png)', 'url(http://www.example.com/image-3.png)' 
    ); 

    var current = 0; 

    function nextBackground() { 
     current++; 
     current = current % backgrounds.length; 
     header.css('background-image', backgrounds[current]); 
    } 
    setInterval(nextBackground, 7500); 

    header.css('background-image', backgrounds[0]); 
}); 
</script> 

在此先感谢

+1

添加'backgrounds.shift( )''在'header.css('background-image',backgrounds [0])之后''''' – Tushar

+0

非常棒的工作谢谢! – sicawebd

回答

0

更改nextBackground功能如下:

function nextBackground() { 
    current++; 
    if (current === backgrounds.length) 
     current = 1; 
    header.css('background-image', backgrounds[current]); 
}