javascript - What's wrong with this DIV background image assignment? -
var bgimages ='myimage.png' var pathtoimg=new array(), i=0 pathtoimg[0]=new image() pathtoimg[0].src=bgimages document.body.background=pathtoimg[i].src
// above works however, when attempted change "body" element "div" id of "slide" fails, fyi, div exists
document.getelementbyid('slide').style.background-image = pathtoimg[i].src
or what's correct syntax setting div background imagine dynamically?
thanks.
- addition, full code
// 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() (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 make bgimage not fixed*/ background-attachment:fixed; background-repeat: no-repeat; /*use center center in place of 300 200 center bg image*/ background-position: 300 200; }
<div id="slide"></div>
change style
using []
instead of .
, :
document.getelementbyid('slide').style['background-image'] = pathtoimg[i].src;
Comments
Post a Comment