javascript - Change Background Image in a loop -
to me, seems simple idea- when click div, background image of div changes. far, i've tried colors (put color names in array, change "box.style.backgroundimage" "box.style.backgroundcolor") works fine colors, not images. idea why?
javascript:
var box = document.getelementbyid('box'), imgs = ['/image.jpg', '/image2.jpg']; box.onclick = function () { img = imgs.shift(); imgs.push(img); box.style.backgroundimage = img; };
html:
<div id='box'></div>
css:
#box { background: url("https://priteshgupta.com/wp-content/uploads/2011/06/html-ipsum.png"); width:200px; height:200px; margin:50px; }
you still need url("...")
:
box.style.backgroundimage = 'url("' + img + '")';
Comments
Post a Comment