JS how to set the background image?

method one:    

A pre-set pattern, when used, just give this object to add a class name on the list.

eg: 

css中:
.bj { background: url(star.jpg) no
-repeat center; background-size: cover; }
js中: oBox.className
= "bj";

Method Two:

JS direct use of element attributes style

. 1 oBox.style.backgroundImage = "URL (star.jpg)"; // set the background of FIG address 
2 oBox.style.backgroundRepeat = "NO-REPEAT"; // set the background is not tiled 
. 3 oBox.style.backgroundPosition = "Center"; // set the background position of FIG 
. 4 oBox.style.backgroundSize = "Cover"; // set the size of the background image

 

Guess you like

Origin www.cnblogs.com/hm-08042/p/11431514.html