gpt4 book ai didi

javascript - 在 Javascript 中获取图像变量的高度和宽度

转载 作者:行者123 更新时间:2023-12-03 03:33:15 24 4
gpt4 key购买 nike

我正在尝试将图片加载到图像变量中,并测试它是横向还是纵向,以便将其放入框架中。用户从下拉列表中选择 URL。但是,下面的代码片段每隔一段时间就会给出 0 的宽度。我在做什么蠢事?

        var Select_Picture = document.getElementById("ddlSelectPicture");
var Client_Picture = document.getElementById("imgClientPicture");
var Temp_Image = new Image();
var Image_Height;
var Image_Width;

Temp_Image.src = Select_Picture.value;

// WAIT FOR PICTURE TO LOAD
while (Temp_Image.width < 1 || Temp_Image.height < 1)
{
Image_Width = Temp_Image.width;
}
Image_Height = Temp_Image.height;

alert(Image_Width + " " + Image_Height);

最佳答案

您正在尝试在加载图像之前读取图像的高度和宽度。
onload 将在 Temp_Image 加载完成时调用,因此图像将具有宽度和高度。

Temp_Image.onload = function() {
console.log(Temp_Image.width);
}
Temp_Image.src = Select_Picture.value;

关于javascript - 在 Javascript 中获取图像变量的高度和宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45991128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com