Skip to content
14:21
Image

Image

Introduction ​

The image component wraps the native image with enhancements: native capabilities such as mode are passed through with the same names, plus loading and error placeholders, fade-in transition, border radius and round shapes.

Examples ​

Basic Usage ​

src is the required image URL. Without a size, the default width is 100% and the default height is 240px. width and height accept a string (used as is, supporting % and other units) and a number (px is appended automatically).

vue
<m-image src="https://cdn.example.com/demo.jpg" />
<m-image src="https://cdn.example.com/demo.jpg" width="160px" height="160px" />
<m-image src="https://cdn.example.com/demo.jpg" :width="120" :height="120" />

Fill Mode ​

Use the mode attribute to set the crop and scale mode, passed through with the same name as the native image mode. The default is aspectFill (crop to fill, keeping the aspect ratio).

vue
<m-image src="https://cdn.example.com/demo.jpg" width="110px" height="110px" mode="scaleToFill" />
<m-image src="https://cdn.example.com/demo.jpg" width="110px" height="110px" mode="aspectFit" />
<m-image src="https://cdn.example.com/demo.jpg" width="110px" height="110px" mode="widthFix" />

Border Radius and Round ​

Use the radius attribute to set the border radius, and the round attribute to render a circle (taking precedence over radius).

vue
<m-image src="https://cdn.example.com/demo.jpg" width="80px" height="80px" round />
<m-image src="https://cdn.example.com/demo.jpg" width="80px" height="80px" radius="8" />
<m-image src="https://cdn.example.com/demo.jpg" width="80px" height="80px" radius="16" />

Loading and Error Placeholder ​

A placeholder icon is shown by default while loading and on failure. Use loading-icon and error-icon to customize the placeholder icons, and icon-size to set their size. Use show-loading and show-error to disable the placeholders, and the loading and error slots for custom placeholder content.

vue
<m-image src="https://cdn.example.com/nonexistent.jpg" width="110px" height="110px" />
<m-image src="https://cdn.example.com/nonexistent.jpg" width="110px" height="110px" error-icon="photo-o" />
<m-image src="https://cdn.example.com/nonexistent.jpg" width="110px" height="110px" :show-error="false" />

Lazy Load and Fade In ​

lazy-load is passed through to the native attribute, effective only under page and scroll-view. When fade is enabled, the image fades in over 300ms after loading; it is enabled by default, and switching the image resets to the loading state automatically.

vue
<m-image src="https://cdn.example.com/demo.jpg" width="130px" height="130px" lazy-load />
<m-image src="https://cdn.example.com/demo.jpg" width="130px" height="130px" :fade="false" />

API ​

Props ​

PropDescriptionTypeDefault
srcImage URLstring-
modeCrop and scale mode, passed through with the same name as the native image modeImageModeaspectFill
lazy-loadWhether to lazy load, passed through to the native lazy-load attributebooleanfalse
fadeWhether to fade in after loadingbooleantrue
widthImage width, px is appended for number, defaults to 100%string | number | nullnull
heightImage height, px is appended for number, defaults to 240pxstring | number | nullnull
radiusBorder radius, px is appended for numberstring | number | nullnull
roundWhether to render a circle, taking precedence over radiusbooleanfalse
show-loadingWhether to show the loading placeholderbooleantrue
show-errorWhether to show the error placeholderbooleantrue
loading-iconLoading placeholder icon nameIconNamephoto
error-iconError placeholder icon nameIconNamephoto-fail
icon-sizePlaceholder icon size, px is appended for pure numbersstring32
custom-classCustom root node class namestring.ClassString-
custom-styleCustom root node inline stylestring-

Events ​

EventDescriptionParameters
clickEmitted when the image is clickedevent: MouseEvent
loadEmitted when the image loads successfully, detail contains the image sizeevent: UniImageLoadEvent
errorEmitted when the image fails to load, detail contains the error messageevent: UniImageErrorEvent

Slots ​

NameDescription
loadingLoading placeholder content
errorError placeholder content
defaultCustom content, rendered as the sibling node after the image

Theming ​

Image styles can be customized with the following SCSS variables (define them directly in the project root uni.scss to override the defaults):

VariableDescriptionDefault
$m-image-backgroundLoading and error placeholder background color#f7f8fa
$m-image-icon-colorPlaceholder icon color#dcdee0

See Theming for the full variable list and override instructions.

Released under the MIT License.