在附加到DOM之前,IMG的宽度?
我已经完成了一些简单的代码任务,发现我可以通过其IMG和IMG的下载的高度和宽度,而无需将其附加到DOM,而无需将其附加到DOM。这是Crossbrowser吗? Coud找不到有关此行为的任何信息。
I've done some simple code tasks and I've found that I can get height and width of downloaded img by its img.height and img.width attributes, without appending it to DOM. Is this crossbrowser? Coudn't find any info on this behavior.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
NaturalWidth
和NaturalHeight
为此要求。它们分别代表图像的原始宽度和高度。NaturalWidth
- 标签中使用的图像的原始宽度。width
- 标签宽度属性的值/默认值。NaturalHeight
- 标签中使用的图像的原始高度。高度
- 标签高度属性的值/默认值。例如:
有关
NaturalHeight
- htttps:/htttps:/ /Developer.mozilla.org/en-us/docs/web/api/htmlemageelement/naturalheight更多有关
naturalwidth
- https://develoverer.mozilla.org/en-us/docs/web/api/web/api/htmlimagelement/htmlimagelement/naturalalalwidthYou can use the
naturalWidth
andnaturalHeight
for that requirement. They represent the original width and height respectively of the image.naturalWidth
- the original width of the image used in tag.width
- the value/default value of width attribute of tag.naturalHeight
- the original height of the image used in tag.height
- the value/default value of height attribute of tag.For example:
More about
naturalHeight
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalHeightMore about
naturalWidth
- https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/naturalWidth