var bgimages ='myImage.png'
var pathToImg=new Array(), i=0
pathToImg[0]=new Image()
pathToImg[0].src=bgimages
document.body.background=pathToImg[i].src
//上述作品 然而,當我試圖將「身體」元素以「滑動」的ID更改爲「DIV」失敗,僅供參考,這個div存在這個DIV背景圖像分配有什麼問題?
document.getElementById('slide').style.background-image = pathToImg[i].src
或者動態設定DIV背景的正確語法是什麼?
謝謝。
- 此外,全碼
// image slideshow script 2
var bgimages=new Array()
bgimages[0]="paper-n.jpg"
bgimages[1]="kn-n.png"
// bgimages[2]="img3.jpg"
//preload images
var pathToImg=new Array()
for (i=0;i<bgimages.length;i++){
pathToImg[i]=new Image()
pathToImg[i].src=bgimages[i]
}
var inc=-1
function bgSlide(){
if (inc < bgimages.length-1)
inc++
else
inc=0
// document.body.background=pathToImg[inc].src
document.getElementById('slide').style['background-image'] = pathToImg[inc].src
// "url('" + pathToImg[i].src + "')";
}
if (document.all||document.getElementById)
window.onload=new Function('setInterval("bgSlide()",3000)')
// slideshow
body {
/*Remove below line to make bgimage NOT fixed*/
background-attachment:fixed;
background-repeat: no-repeat;
/*Use center center in place of 300 200 to center bg image*/
background-position: 300 200;
}
<div id="slide"></div>
嗨,請與我們分享更多的代碼。它可能是你運行你的JavaScript代碼到早(div之前)或ID爲'slide'的div不存在。 – peebee
非常好,謝謝。我試圖添加DIV除了BODY風格無濟於事。 – user963063
@Dario有正確的答案,我修改了你的代碼,並把它作爲一個片段的答案。 – peebee