简体   繁体   English

根据父级div大小调整图像大小或裁剪

[英]Image resize or cropped based on the Parent div size

I have div which have 600px width and 631px height initially . 我的div最初具有600px的宽度和631px的高度。

There is two input field where user can change width and height . 用户可以在两个输入字段中更改宽度和高度。 Inside this div i have an image 在这个div内,我有一张图片

so the structure 所以结构

<div class="container-div">
 <img src="img.jpg" class="new-img">
</div>

so when user resize the div size , image also need to be resize proportionally . 因此,当用户调整div大小时,图像也需要按比例调整大小 It will not be stretched . 它不会被拉伸。

How i can do this ? 我该怎么做?

Note : i wrote the code for to resize the div after user entering their width and height . 注意:在用户输入宽度和高度后,我编写了用于调整div大小的代码。 so i actually try to get code after div resize . 所以我实际上尝试在div调整大小后获取代码。

$(".input1, .input2").on("change",function(){
   measurement_change();
});


function measurement_change(){
 after calculation i set new_width and new_height 
 $(".container-div").css("width", new_width);
 $(".container-div").css("height", new_height);

   After this i hav e to resize image propotionally 

}

Please check. 请检查。

$(".input1, .input2").on("change",function(){
   var width = $('.input1').val();
   var height = $('.input2').val();
   $(".container-div").css("width", width);
   $(".container-div").css("height", height);
   $(".new-img").css("max-width",width);
});

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

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