我已經嘗試了幾件事情,但迄今沒有運氣。動態更改CSS背景圖像
我在尋找的是一種讓我的HTML標頭標籤每隔幾秒更換背景圖片的方法。任何解決方案都是受歡迎的,只要它不復雜。
我現在有這樣的代碼,以及鏈接到JQuery的:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
$(function() {
var header = $(‘.mainHeader’);
var backgrounds = new Array(
‘url(img/rainbow.jpg)’,
‘url(img/chickens_on_grass.jpg)’
‘url(img/cattle_on_pasture.jpg)’
‘url(img/csa_bundle.jpg)’
);
var current = 0;
function nextBackground() {
header.css(‘background’,backgrounds[current = ++current % backgrounds.length]);
setTimeout(nextBackground, 10000);
}
setTimeout(nextBackground, 10000);
header.css(‘background’, backgrounds[0]);
});
我的HTML頭:
<header class="mainHeader"></header>
和CSS:
.mainHeader {
background: no-repeat center bottom scroll;
-webkit-background-size: cover;
-moz-background-size: cover;
-o-background-size: cover;
background-size: cover;
min-height: 150px;
padding-top: 2%;
font-size: 100%;
}
現在我現在有背景圖像了。
期待建議。
你的陣列是錯誤的...有幾個逗號丟失.. – gvee
引號字符串是錯誤的了。 http://jsfiddle.net/4LFnK/ – amsross