gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 16:08:12 24 4
gpt4 key购买 nike

在加载图像本身之前,我需要获取图像的高度和宽度。所以我使用 jquery .load() 方法。

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

但问题是我需要根据固定宽度确定高度。假设图像的大小为 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

})

关于javascript - 根据.load()中的宽度获取图像高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30508284/

24 4 0
文章推荐: javascript - radio 上的淡入/淡出
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com