Using an image object as a background

  • Post author:
  • Post category:PMF
  • Post comments:0 Comments

https://stackoverflow.com/questions/9790347/set-an-image-object-as-a-div-background-image-using-javascript

var imgSrcs = [...];   // array of URLs
var myImages = [], img;
for (var i = 0; i < 4; i++) {
    img = new Image();
    img.onload = function() {
        // decide which object on the page to load this image into
        // this part of the code is missing because you haven't explained how you
        // want it to work
        var div0 = document.getElementById('theDivId');
        div0.style.backgroundImage = "url(" + this.src + ")";
    };
    img.src = imgSrcs[i];
    myImages[i] = img;
}

Leave a Reply