Javascript 根据.load()中的宽度获取图像高度
我需要得到图像的高度和宽度,然后再加载图像本身。所以我使用jquery.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
$('
但问题是,我需要根据固定宽度确定高度。假设图像大小为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
})