您好,我有一个图片上传表单,当选择上传图片时,它还会显示预览。
$( function() {
var inputLocalFont = document.getElementById("user_file");
inputLocalFont.addEventListener("change",previewImages,false);
function previewImages(){
var fileList = this.files;
var anyWindow = window.URL || window.webkitURL;
for(var i = 0; i < fileList.length; i++){
var objectUrl = anyWindow.createObjectURL(fileList[i]);
$('.new-multiple').append('<div class="img-div"><img src="' + objectUrl + '" class="newly-added" /></div>');
window.URL.revokeObjectURL(fileList[i]);
}
$( ".img-div" ).draggable();
$( ".img-div" ).resizable();
}
});
.new-multiple{
width:400px !important;
height:400px !important;
background:white;
border:2px solid red;
overflow:hidden;
}
.img-div{
width:200px;
height:200px;
}
.newly-added{
width:100%;
height:100%;
}
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input name="user_file[]" id="user_file" style="position: relative;overflow: hidden" multiple="" type="file">
<div class="new-multiple"></div>
我为图像编写了可调整大小和可拖动的功能。
但是有什么办法吗
(1) 当我点击一张图片时,它会显示为选中状态 [可能是选中的额外阴影,点击外面的阴影也会消失] 和
(2) 当我按下键盘上的删除按钮时,所选图像被删除。当我按下撤消按钮时,它会出现吗?
请帮忙。
UPDATE
.img-selected{
box-shadow: 1px 2px 6px 6px rgb(206, 206, 206);
}
$(".newly-added").on("click", function(e) {
$(".newly-added").removeClass("img-selected");
$(this).addClass("img-selected");
e.stopPropagation()
});
$(document).on("click", function(e) {
if ($(e.target).is(".newly-added") === false) {
$(".newly-added").removeClass("img-selected");
}
});
最佳答案
试试这个:
我添加了这个 jquery 代码
$(document).on('click','img',function(){
$('img').removeClass('shadow');
$(this).toggleClass("shadow");
});
和影子CSS
.shadow{
box-shadow: 10px 10px 5px #888888;
}
$( function() {
var inputLocalFont = document.getElementById("user_file");
inputLocalFont.addEventListener("change",previewImages,false);
function previewImages(){
var fileList = this.files;
var anyWindow = window.URL || window.webkitURL;
for(var i = 0; i < fileList.length; i++){
var objectUrl = anyWindow.createObjectURL(fileList[i]);
$('.new-multiple').append('<div class="img-div"><img src="' + objectUrl + '" class="newly-added" /></div>');
window.URL.revokeObjectURL(fileList[i]);
}
$( ".img-div" ).draggable();
$( ".img-div" ).resizable();
}
$(document).on('click','img',function(){
$('img').removeClass('shadow');
$(this).toggleClass("shadow");
});
});
.new-multiple{
width:400px !important;
height:400px !important;
background:white;
border:2px solid red;
overflow:hidden;
}
.shadow{
box-shadow: 10px 10px 5px #888888;
}
.img-div{
width:200px;
height:200px;
}
.newly-added{
width:100%;
height:100%;
}
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<input name="user_file[]" id="user_file" style="position: relative;overflow: hidden" multiple="" type="file">
<div class="new-multiple"></div>
关于javascript - 使用 jquery 撤消、重做、删除所选图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46520398/