这个DIV背景图像分配有什么问题?

时间:2016-08-23 19:57:41

标签: javascript html css

var bgimages ='myImage.png'

var pathToImg=new Array(), i=0
pathToImg[0]=new Image()
pathToImg[0].src=bgimages

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

//上述作品 然而,当我试图改变"身体"元素到" DIV" id为" slide"它失败了,fyi,这个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)')
&#13;
// 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;
}
&#13;
<div id="slide"></div>
&#13;
&#13;
&#13;

4 个答案:

答案 0 :(得分:1)

使用style而不是[]更改.,如下所示:

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

答案 1 :(得分:1)

尝试使用backgroundImage而不是background-image。

document.getElementById('slide').style.backgroundImage = pathToImg[i].src

与此处的示例相似:http://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_style_backgroundimage2

答案 2 :(得分: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>

答案 3 :(得分:0)

您忘记了style

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

background是一个CSS主义,而不是<body>标签本身的一部分。