循环更改背景图片

Mancestr

对我来说,这似乎是一个简单的想法-单击div时,该div的背景图像就会改变。到目前为止,我已经尝试过使用颜色了(将颜色名称放入数组中,将“ box.style.backgroundImage”更改为“ box.style.backgroundColor”),它可以很好地处理颜色,但不适用于图像。知道为什么吗?

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;
}
黑暗猎鹰

您仍然需要url("...")

box.style.backgroundImage = 'url("' + img + '")';

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章