2015-10-07 147 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]); 
}