简体   繁体   English

如何设置图像宽度/高度

[英]How to set image Width/Height

I have what I think is very simple code and I expect it to modify the img width/height but it is not working. 我有我认为非常简单的代码,我希望它修改img宽度/高度,但它不起作用。

I use createObjectURL, then get the width/height of that image. 我使用createObjectURL,然后获取该图像的宽度/高度。 Then, I will calculate the width/height so that I can create a thumbnail in the proper proportion (not distort the original image but just make it smaller). 然后,我将计算宽度/高度,以便我可以以适当的比例创建缩略图(不会扭曲原始图像,只是将其缩小)。 For now, I am just hard-coding values to see if it works, but it does not. 现在,我只是硬编码值,看看它是否有效,但事实并非如此。

Then, I load the image with the new values. 然后,我用新值加载图像。

But it does not change the original height/width that I set for the img in the css. 但它不会改变我在css中为img设置的原始高度/宽度。

A fiddle is here: 这里有一个小提琴:

All, help, as always is appreciated. 所有,帮助,一如既往地受到赞赏。 Belows is the relevant block of code. Belows是相关的代码块。 The id="fileDisplay" is the container. id =“fileDisplay”是容器。 The background url will be a "loading" image. 背景网址将是“加载”图片。 id="imgDisaply" is the img component where the image gets loaded, which covers up the "loading" background. id =“imgDisaply”是图像加载的img组件,它掩盖了“加载”背景。 It was the only way I could figure out how to give a "loading" image as feedback to the user as the image loads. 这是我能够弄清楚如何在图像加载时将“加载”图像作为反馈给用户的唯一方法。

imgSrc = window.URL.createObjectURL(this.files[0]);
getImgSize(imgSrc);
document.getElementById("imgDisplay" + justNumber).width="500";
document.getElementById("imgDisplay" + justNumber).height="200";
document.getElementById("imgDisplay" + justNumber).src = imgSrc;

You are missing the '.style' and specification of size attr (px, em, etc.). 您缺少'.style'和大小attr(px,em等)的规范。

I believe this may be what you were going for: 我相信这可能是你想要的:

imgSrc = window.URL.createObjectURL(this.files[0]);
getImgSize(imgSrc);
document.getElementById("imgDisplay" + justNumber).style.width="500px";
document.getElementById("imgDisplay" + justNumber).style.height="200px";
document.getElementById("imgDisplay" + justNumber).src = imgSrc;

In DOM, you need to use .style to mention styles. 在DOM中,您需要使用.style来提及样式。

document.getElementById("imgDisplay" + justNumber).style.width="500";
document.getElementById("imgDisplay" + justNumber).style.height="200";

Ok so here is an image taken from a source and a alert message show us her width and height hope this snippet can help you: 好的,所以这里是从源和一个警报消息中获取的图像向我们展示她的宽度和高度希望这个片段可以帮助您:

 var image = document.createElement('img'); var image_url = "https://images-na.ssl-images-amazon.com/images/G/01/img15/other-services/billboard/29597_os_int_fr_showcase_1500x300._CB288675343_.jpg"; image.src = image_url; image.onload = function() { alert("width : " +image.width+ " px"); alert("height : " +image.height+ " px"); } 

Hope it help 希望它有所帮助

Either ways of setting width/height is somehow correct. 设置宽度/高度的任何方式都是正确的。 Both of them do work, but in different ways. 它们都有效,但方式不同。

document.getElementById("imgId").style.width="500px";

results in an img with inline style attribute: 导致带有内联样式属性的img:

<img src="..." style="width: 500px" />

While document.getElementById("imgId").setAttribute("width", "500"); document.getElementById("imgId").setAttribute("width", "500"); creates an img with width attribute(no px at the end): 创建一个width属性的img(最后没有px):

<img src="..." width="500" />

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

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