Anyone know how to add css background without replacing the previous background with js?
for example,
<div style = "background: url (image.png)"> </ div>
after added
<div style = "background: url (image2.png), url (image.png)"> </ div>
Anyone know how to add css background without replacing the previous background with js?
for example,
<div style = "background: url (image.png)"> </ div>
after added
<div style = "background: url (image2.png), url (image.png)"> </ div>
You can make use of CSS classes and switch between classes when required.
.class_one {
background: url (image.png);
}
.class_two {
background: url (image2.png), url (image.png);
}
Here is the Javascript solution:
var backgroundStyle = document.querySelector("#container").style.background;
document.querySelector("#container").style.background = backgroundStyle + ", url('http://res.cloudinary.com/demo/image/upload/sample.jpg')";
<div style = "background: url('http://www.brightlinkprep.com/wp-content/uploads/2014/04/sample.jpg')" id="container" >
</div>
You can verify it using the Developer Tools / FireBug.
Just get the background value, and append another url(img_url)
var my_div= document.querySelector("my_div");
if( my_div.style.background == "" )
my_div.style.background= "url(image.png)";
else
my_div.style.background+= ",url(image2.png)";
https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Using_multiple_backgrounds