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

后端 未结 9 2235
执念已碎
执念已碎 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条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-02 16:59

    I think this will help, Base64 is addressed by CSS in a different way, you should set the data type of the image to base64, this will help the CSS to change the base64 to image and display it to the user. and you can use this from javascript by assigning the background image using the jquery script, it will automatically change the base64 to mage and display it

    url = "data:image;base64,"+data.replace(/(\r\n|\n|\r)/gm, "");
    $("body").css("background-image", "url('" + url.replace(/(\r\n|\n|\r)/gm, "") + "')");

提交回复
热议问题