您好我有一个图片上传表单,当选择上传图片时,它也会显示预览。
$( 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();
}
});
&#13;
.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%;
}
&#13;
<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>
&#13;
我为图像编写了可调整大小的可拖动功能。
但是有什么办法
(1)当我点击一个图像时,它将显示为已选择[可能是所选的额外阴影,也可以点击外部阴影消失]和
(2)当我按下键盘上的删除按钮时,删除所选图像。当我按下撤销按钮然后它会出现回来?
请帮忙。
更新
.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");
}
});
答案 0 :(得分:0)
试试这个:
我添加了这个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>