タイトルを押しても写真が表示されないです。
選択はできるのですが、どう改善すれば良いでしょうか?
アドバイスくださると嬉しいです。
HTML
<input type="file" id="image-id" accept="image/*;capture=camera"> <label for="image-id" class="imageBox"> <span class="title">タイトル</span> </label>
CSS
input[type="file"] { display: none; } label.imageBox { position: relative; width: 200px; height: 200px; border-width: 1px; border-style: solid; border-radius: 10px; background-position: center center; background-size: cover; -webkit-box-shadow: 0 0 1px 1px rgba(0, 0, 0, .3); display: block; } label.imageBox > span.title { position: absolute; top: 50%; left: 50%; -webkit-transform: translate(-50%, -50%); -moz-transform: translate(-50%, -50%); transform: translate(-50%, -50%); }
javascript
$(document).ready(function () { $(document).on("click", "input[type='file']", function () { var id = $(this).attr("id"); $("#" + id + " + label").css("background-image", ""); $(this).val(""); }); $(document).on("change", "input[type='file']", function () { var id = $(this).attr("id"); $("#" + id + " + label").css("background-image", ""); var files = !!this.files ? this.files : []; if (!files.length || !window.FileReader) { return; } if (/^image/.test(files[0].type.toLowerCase())) { var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onloadend = function () { $("#" + id + " + label").css("background-image", "url(" + this.result + ")"); }; } }); });
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2022/10/04 07:42