【发布时间】:2018-03-13 12:41:16
【问题描述】:
您好,我有一个可调整大小的 jquery div,以及此 div 内的图像。
$( 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();
$(".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");
}
});
}
});
.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%;
}
.img-selected{
box-shadow: 1px 2px 6px 6px rgb(206, 206, 206);
}
<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>
https://jsfiddle.net/vd11qyzv/7/
这里我必须手动设置图像宽度、div 宽度以进行初始加载
.img-div{
width:200px;
height:200px;
}
.newly-added{
width:100%;
height:100%;
}
如果我没有使用这个 CSS,那么调整大小将不起作用。
但我不想这样做。 因为上传的图片会因为这个手动的宽高而被拉伸或放大。原始图像尺寸将丢失。
我们如何解决这个问题?
【问题讨论】:
标签: javascript jquery css jquery-ui jquery-plugins