Image() - Web APIs 编辑
The Image()
constructor creates a new HTMLImageElement
instance. It is functionally equivalent to document.createElement('img')
.
Disambiguation: image()
, the CSS function notation.
Syntax
var htmlImageElement = new Image(width, height);
Parameters
width
- The width of the image (i.e., the value for the
width
attribute). height
- The height of the image (i.e., the value for the
height
attribute).
Usage note
The entire bitmap is loaded regardless of the sizes specified in the constructor. The size specified in the constructor is reflected through the properties HTMLImageElement.width
and HTMLImageElement.height
of the resulting instance. The intrinsic width and height of the image in CSS pixels are reflected through the properties HTMLImageElement.naturalWidth
and HTMLImageElement.naturalHeight
. If no size is specified in the constructor both pairs of properties have the same values.
Examples
var myImage = new Image(100, 200);
myImage.src = 'picture.jpg';
document.body.appendChild(myImage);
This would be the equivalent of defining the following HTML tag inside the <body>
:
<img width="100" height="200" src="picture.jpg">
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of 'Image()' in that specification. | Living Standard |
Browser compatibility
BCD tables only load in the browser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论