是否可以使用javascript同时设置背景和背景图像?

是否可以使用javascript同时设置背景和背景图像?,javascript,css,Javascript,Css,每当我试图通过javascript设置background和background image属性时,我只获得元素的background peropery。为什么会这样?我错过什么了吗 divelement.style.background = "url('one.png')" divelement.style.backgroundImage = "url('two.png')" // only gets.. <div style="background-image: url("one.

每当我试图通过javascript设置background和background image属性时,我只获得元素的background peropery。为什么会这样?我错过什么了吗

divelement.style.background = "url('one.png')"
divelement.style.backgroundImage = "url('two.png')"

// only gets..

<div style="background-image: url("one.png");">
divelement.style.background=“url('one.png')”
divelement.style.backgroundImage=“url('two.png')”
//只有。。

您可以在一条语句中设置多个背景:

divelement.style.background = "url('one.png'), url('two.png')";

在您的代码中,您正在用另一个覆盖一个背景,因为
背景
背景图像
css属性都可以设置背景图像。

可以在
样式
属性处设置多个
背景图像
。您可以使用
backgroundPosition
调整图像位置以显示多个
backgroundImage

var div=document.querySelector(“div”);
div.style.backgroundImage=“url(http://lorempixel.com/50/50/technics),\
网址(http://lorempixel.com/50/50/nature)";
div.style.backgroundPosition=“0px 0px,50px 0px”
div{
宽度:100px;
高度:50px;
背景重复:无重复;
}

这两个背景图像应该如何显示?使用相同的位置?发生这种情况是因为
background
是一个速记属性,它还设置
背景图像
,因此覆盖it@LGSon完美的解释:)就要发布同样的东西了。谢谢LGSon,您应该发布答案。@narusawa已经有足够的答案涵盖了它。经过良好论证的答案:)或者您可以在一个
div
元素上使用具有透明度的四个背景图像使用
backgroundPosition
?我在本教程中看到背景和背景图像属性一起使用:有没有办法通过javascript实现相同的效果?@narusawa您可以在单个属性中设置多个
backgroundImage
值,如stacksnippets、JSFIDLE所示
javascript
实际上并不需要渲染滑动或旋转效果;您可以使用
css
transition
animation
:hover
来实现一种效果,该效果似乎可以设置背景图像的动画或更改背景图像。可以使用
requestAnimationFrame
或递归渲染相同或类似的效果。