简体   繁体   English

基于jQuery resizable()函数中内部图像的宽度和高度的div宽度和高度

[英]Div width and height based on inner image width & height in jquery resizable() function

Hi i have a resizable jquery div ,and an image inside this div . 嗨,我有一个可调整大小的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/ https://jsfiddle.net/vd11qyzv/7/

Here i have to set the image width , div width manually for the initial loading 在这里我必须为初始加载手动设置图像宽度,div宽度

.img-div{
   width:200px;
   height:200px;
 } 
 .newly-added{
    width:100%;
    height:100%;
}  

if i didn't use this css then resizable will not work. 如果我不使用此CSS,则无法调整大小。

But i don't want to do this . 但是我不想这样做。 Because the uploaded image will become stretched or enlarged because of this manual width and height . 因为此手动的宽度和高度会导致上载的图像被拉伸或放大。 Original image dimension will lost. 原始图像尺寸将丢失。

How can we solve this ? 我们该如何解决呢?

First, there is lots of useful info here: How to get image size (height & width) using JavaScript? 首先,这里有很多有用的信息: 如何使用JavaScript获取图像大小(高度和宽度)?

Please research your issue first before posting. 发布前,请先研究您的问题。

Second, using the example I offered in the previous question you asked, we can gather this detail by using JavaScript, var img = new Image(); 其次,使用我在上一个问题中提供的示例,我们可以使用JavaScript来收集此详细信息var img = new Image(); , to create the image object, not in the DOM or Markup, and then get the dimensions from it once it is loaded. ,而不是在DOM或标记中创建图像对象,然后在加载后从中获取尺寸。

Snippet 片段

  var imgDim = {
    width: 0,
    height: 0
  };
  var newImg = new Image();
  $(newImg).attr({
    src: objectUrl,
    id: "img-" + key,
    class: "newly-added"
  }).load(function(e) {
    imgDim.width = this.width;
    imgDim.height = this.height;
    $(newImg).appendTo($newDiv).css({
      width: imgDim.width + "px",
      height: imgDim.height + "px"
    });
  });
  $(newImg).resizable();
  window.URL.revokeObjectURL(val);

Working Example: https://jsfiddle.net/Twisty/vd11qyzv/12/ 工作示例: https//jsfiddle.net/Twisty/vd11qyzv/12/

Update 更新资料

If you want to resize the image and retain the ratio, you can do this using a set Width or Height, and calculating a ratio value from that. 如果要调整图像大小并保留比例,可以使用设置的“宽度”或“高度”并从中计算出比例值来实现。 For example, if you wanted to set the width to 200 pixels: 例如,如果要将宽度设置为200像素:

Nw = 200
Nh = Ih * (200/Iw)

Snippet 片段

imgDim.width = 200;
imgDim.height = Math.floor(this.height * (200 / this.width));

If your original dimensions were 1536 x 2048, they would result in 200 x 266. Working example: https://jsfiddle.net/Twisty/vd11qyzv/15/ 如果您的原始尺寸为1536 x 2048,则结果为200 x266。工作示例: https//jsfiddle.net/Twisty/vd11qyzv/15/

I have made few changes in our code and update js fiddle link is https://jsfiddle.net/prashantchaurasia/dksqu2hb/6/ 我对我们的代码进行了一些更改,并更新了js小提琴链接, 网址https://jsfiddle.net/prashantchaurasia/dksqu2hb/6/

css updated of following classes
.new-multiple{
  width:auto;
  height:auto;
  background:white;
  border:2px solid red;
  overflow:hidden;
  display: inline-block;
}
.img-div{

} 
.newly-added{

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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