Skip to main content
Version: 3.x

Image

Image. This component supports JPG, PNG, and SVG images. Cloud file IDs are supported from 2.3.0 and later.

Note: To implement the mode feature of the mini program, a div container is used in the H5 component to corp the display area of the internal img, so please do not use the element selector to reset the style of the img!

Reference

Type

ComponentType<ImageProps>

Examples

export default class PageView extends Component {
constructor() {
super(...arguments)
}

render() {
return (
<View className='components-page'>
<Image
style='width: 300px;height: 100px;background: #fff;'
src='nerv_logo.png'
/>
<Image
style='width: 300px;height: 100px;background: #fff;'
src='https://camo.githubusercontent.com/3e1b76e514b895760055987f164ce6c95935a3aa/687474703a2f2f73746f726167652e333630627579696d672e636f6d2f6d74642f686f6d652f6c6f676f2d3278313531333833373932363730372e706e67'
/>
</View>
)
}
}

ImageProps

PropertyTypeDefaultRequiredDescription
srcstringYesThe image resource address
mode"scaleToFill" | "aspectFit" | "aspectFill" | "widthFix" | "heightFix" | "top" | "bottom" | "center" | "left" | "right" | "top left" | "top right" | "bottom left" | "bottom right""scaleToFill"NoSpecifies the clipping mode or the scale mode of an image
webpbooleanfalseNoWebP format is not parsed by default, only web resources are supported.
lazyLoadbooleanfalseNoEnables lazy loading of images. An image is not loaded until it enters a specific range (within three consecutive screens).
showMenuByLongpressbooleanfalseNoEnables the feature of displaying the Mini Program code menu when an image is tapped and held.
onErrorBaseEventOrigFunction<onErrorEventDetail>NoTriggered when an error occurs. event.detail = {errMsg}
onLoadBaseEventOrigFunction<onLoadEventDetail>NoTriggered when an image is completely loaded. event.detail = {height, width}
imgPropsImgHTMLAttributes<HTMLImageElement>NoAdditional attributes for `img` tags

Property Support

PropertyWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Micro-AppH5React Native
ImageProps.src✔️✔️✔️✔️✔️✔️
ImageProps.mode✔️✔️✔️✔️✔️✔️(Partial support scaleToFill, aspectFit, aspectFill, widthFix)
ImageProps.webp✔️
ImageProps.lazyLoad✔️✔️✔️✔️
ImageProps.showMenuByLongpress✔️
ImageProps.onError✔️✔️✔️✔️✔️✔️
ImageProps.onLoad✔️✔️✔️✔️✔️✔️
ImageProps.imgProps✔️

mode

Valid values of mode

ValueDescription
scaleToFillA scale mode, where the image is scaled without maintaining the aspect ratio to fully stretch to fill the screen with elements of the image.
aspectFitA scale mode, where the image is scaled with the aspect ratio unchanged to fully display its longer edge. In this case, the image can be completely displayed.
aspectFillA scale mode, where the image is scaled with the aspect ratio unchanged to fully display its shorter edge. In this case, the image is completely displayed in the horizontal or vertical direction, and it is truncated in the other direction.
widthFixA scale mode, where the width of the image remains unchanged with the height adjusted automatically, and the aspect ratio of the original image is maintained.
heightFixA scale mode, where the height of the image remains unchanged with the width adjusted automatically, and the aspect ratio of the original image is maintained.
topA clipping mode, where only the upper area of the image is displayed without scaling.
bottomA clipping mode, where only the lower area of the image is displayed without scaling.
centerA clipping mode, where only the central area of the image is displayed without scaling.
leftA clipping mode, where only the left area of the image is displayed without scaling.
rightA clipping mode, where only the right area of the image is displayed without scaling.
top leftA clipping mode, where only the upper left area of the image is displayed without scaling.
top rightA clipping mode, where only the upper right area of the image is displayed without scaling.
bottom leftA clipping mode, where only the lower left area of the image is displayed without scaling.
bottom rightA clipping mode, where only the lower right area of the image is displayed without scaling.

API Support

APIWeChat Mini-ProgramBaidu Smart-ProgramAlipay Mini-ProgramByteDance Micro-AppH5React Native
Image✔️✔️✔️✔️✔️✔️