Is there a way to set background-image as a base64 encoded image?

后端 未结 9 2284
执念已碎
执念已碎 2020-12-02 16:09

I want to change background dynamically in JS and my set of images is in base64 encoded. I try:

document.getElementById(\"bg_image\").style.backgroundImage =         


        
9条回答
  •  猫巷女王i
    2020-12-02 16:55

    Adding this trick to gabriel garcia's following solution -

    var img = 'data:image/png;base64, ...'; //place ur base64 encoded img here
    document.body.style.backgroundImage = 'url(' + img + ')';
    

    However, in my case this wasn't working. Moving url into the img variable did the trick. SO the final code looked like this

    var img = "url('data:image/png;base64, "+base64Data + "')";
    document.body.style.backgroundImage = img; 
    

提交回复
热议问题