2016-08-23 58 views
2
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>

+1

嗨,請與我們分享更多的代碼。它可能是你運行你的JavaScript代碼到早(div之前)或ID爲'slide'的div不存在。 – peebee

+0

非常好,謝謝。我試圖添加DIV除了BODY風格無濟於事。 – user963063

+0

@Dario有正確的答案,我修改了你的代碼,並把它作爲一個片段的答案。 – peebee

回答

0

你忘style

document.body.style.background=pathToImg[i].src 
       ^^^^^ 

background是CSS主義,不是那是的一部分標籤本身。

+0

下面的代碼有效,所以讓我們把它放在一邊,而不是讓我們把注意力集中在一個可能的問題上,tks。 document.body.background = pathToImg [i] .src – user963063

1

更改style使用[]代替.,像這樣:

document.getElementById('slide').style['background-image'] = pathToImg[i].src; 
+0

樣式屬性是camelcase。 – Barmar

+0

@Protectator,與以前相同的錯誤。感謝壽。 – user963063

1

根據這個link的文檔,你應該寫這樣的:

document.getElementById("slide").style["background-image"] = "url('" + pathToImg[i].src + "')"; 

編輯修改 工作代碼

// image slideshow script 2 
 
var bgimages=new Array() 
 
bgimages[0]="http://dummyimage.com/200x123/0f0/00f" 
 
bgimages[1]="http://dummyimage.com/200x123/f00/00f" 
 
// 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 
 

 
    // BOTH of following works 
 
    //document.getElementById('slide').style.backgroundImage = "url("+pathToImg[inc].src+")"; 
 
    document.getElementById('slide').style['background-image'] = "url('"+pathToImg[inc].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; 
 
} 
 

 
#slide { 
 
    width : 200px; 
 
    height : 123px; 
 
}
<div id="slide">This is my div</div>

+0

大家好,我嘗試了每個解決方案推薦,但是,我得到的錯誤,實際上相同的錯誤,錯誤味精:無法讀取null的屬性'風格'謝謝。 – user963063

+1

你確定你的html中有id「slide」的元素嗎?我發佈的代碼作品,請檢查以下鏈接:http://jsbin.com/mekaxa/edit?js,output – Dario