Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 根据.load()中的宽度获取图像高度_Javascript_Jquery - Fatal编程技术网

Javascript 根据.load()中的宽度获取图像高度

Javascript 根据.load()中的宽度获取图像高度,javascript,jquery,Javascript,Jquery,我需要得到图像的高度和宽度,然后再加载图像本身。所以我使用jquery.load()方法 $(' 但问题是,我需要根据固定宽度确定高度。假设图像大小为400*417,我需要根据200而不是400确定高度。为此,我添加了 $('<img />').attr({ 'src':someURL, 'width':200}).load(function(){ //Get height and width }) $(' 但是上面的代码再次给了我417的高度,而不是200的宽度。我正在寻找j

我需要得到图像的高度和宽度,然后再加载图像本身。所以我使用jquery.load()方法

$('
但问题是,我需要根据固定宽度确定高度。假设图像大小为400*417,我需要根据200而不是400确定高度。为此,我添加了

$('<img />').attr({
'src':someURL,
'width':200}).load(function(){
  //Get height and width
})
$('
但是上面的代码再次给了我417的高度,而不是200的宽度。我正在寻找js/jquery/angualrjs。请帮助


提前感谢:)

一点数学就可以了。一旦你得到了实际的图像尺寸,应用你的比例来得到你想要的高度

例如:

$('<img />').attr('src',someURL).load(function(){
  console.log(this.width); // prints out 400
  console.log(this.height); // prints out 417

  var widthIWant = 200;

  var heightIWant = this.height * widthIWant / this.width; // Gives you 208.5

})

$('@PardeepDhingra我可以告诉你,你不明白我的问题。我需要图像加载前的高度,它本身就像一个符咒:)非常感谢:D
$('<img />').attr('src',someURL).load(function(){
  console.log(this.width); // prints out 400
  console.log(this.height); // prints out 417

  var widthIWant = 200;

  var heightIWant = this.height * widthIWant / this.width; // Gives you 208.5

})