简体   繁体   中英

How to dynamically set background image url in CSS using Javascript

I've been trying to make this work for a little while now.

I thought the following code would work since I'm getting the value from the input and setting the background-image URL to said value.

Thanks!

The code inside of the head tag.

<script  type="text/javascript">

 function loadImg() {

  var imageUrl = $('#hostImage').attr('value')

  document.getElementById("upload-success-bg").style.backgroundImage=imageUrl

}

</script>

<style>

 #upload-success-bg {
 background-image: url();
 }

</style>

Input field code

<div class="status">

 <input class="image-url" type="text" id="hostImage" name="hostImage" required="true" 
 value="URL LOADS HERE">

</div>

Where I would like to the image to show

<div class="dropzone upload-success" id="upload-success-bg">
            <div class="info"><p>Drag image file here</p><p>Or click here to select image</p></div>
<input type="file" required="" class="input" accept="image/*"></div>

如果您希望将URL用于backgound background-colorbackground-color CSS属性,则即使在JavaScript中也必须使用url()语法,因此将代码更改为以下内容应该可以:

document.getElementById("upload-success-bg").style.backgroundImage = "url(" + imageUrl + ")"

In jquery, you can do it this way:

 function loadImg() {

  var imageUrl = $('#hostImage').attr('value')

  $("#upload-success-bg").css("background-image", "url(" + imageUrl + ")");

}

A File object does not have a URL property. A Blob URL or data URL can be created which points to the uploaded file. A Blob URL s lifetime is linked to the document which created the URL. A data URL string

data:[<mediatype>][;base64],<data>

can be opened at a different window or browser.

You can use FileReader to convert File object to a data URL and set the <input type="text"> value to the FileReader instance result .

 const input = document.querySelector("#file"); const [label] = input.labels; const upload = document.querySelector("#upload-success-bg"); const uploadURL = document.querySelector("#hostImage"); const reader = new FileReader(); reader.addEventListener("load", e => { const {result} = reader; upload.style.backgroundImage = `url(${result})`; hostImage.style.width = `calc(${result.length}px)`; hostImage.value = result; }); input.addEventListener("change", e => { const [file] = input.files; console.log(file) if (file && /^image/.test(file.type)) { reader.readAsDataURL(file); } }); 
 #file { display: none; } label[for="file"] { white-space: pre; } 
 <div class="dropzone upload-success" id="upload-success-bg"> <label class="info" for="file"> Drag image file here Or click here to select image </label> <input type="file" required="" id="file" class="input" accept="image/*"></div> <div class="status"> <input class="image-url" type="text" id="hostImage" name="hostImage" required="true" readonly="true" value="URL LOADS HERE"> </div> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM