Image picture

introduce

Based on the image component of uniapp, it provides loading prompts and loading failure prompts.

Code Demo

Basic usage

The basic usage is the same as the image component of the native uniapp, and you can set the src, width, height and other attributes.

  <z-image width="100" height="100" src="https://cdn.zebraui.com/zebra-ui/images/demo.jpg" />

Fill mode

The image filling mode can be set through the mode attribute, which is equivalent to the mode attribute of uniapp. For optional values, see image of uniapp components.

<z-image
   width="150rpx"
   height="150rpx"
   :mode="item"
   :src="imageUrl"
/>

Crop

The image cropping position can be set through the mode attribute. For optional values, see the image component of uniapp.

<z-image
   width="150rpx"
   height="150rpx"
   :mode="item"
   :src="imageUrl"
/>

Circle picture

You can set the image to be rounded through the round attribute

  <z-image round width="100" height="100" :src="imageUrl" />

Loading prompt

The Image component provides a default loading prompt and supports custom content through the loading slot.

  <z-image show-loading width="100" height="100" />
  <!-- Custom loading prompt -->
  <z-image show-loading width="100" height="100">
     <template #loading>
       <z-loading type="spinner" size="40rpx" />
     </template>
   </z-image>

Loading failure prompt

The Image component provides a default loading failure prompt and supports custom content through the error slot.

<z-image show-error width="100" height="100" src="x" />
<!-- Customized loading failure prompt -->
<z-image show-error width="100" height="100" src="x">
   <template #error> Loading failed </template>
</z-image>

API

Props

ParametersDescriptionTypeDefault value
srcImage linkstring-
modeImage cropping and scaling mode, equivalent to uniapp's mode attributestringfill
widthWidth, default unit is pxnumber | string-
heightheight, default unit is pxnumber | string-
radiusFillet size, default unit is pxnumber | string0
roundWhether to display as a circlebooleanfalse
blockWhether to set the root node as a block-level element, by default an inline-block elementbooleanfalse
show-errorWhether to display image loading failure promptsbooleantrue
show-loadingWhether to display the image loading promptbooleantrue
error-iconThe icon name or image link prompted when failure occurs, which is equivalent to the name attribute of the Icon componentstringerror-fill
loading-iconThe icon name or image link prompted when loading, equivalent to the name attribute of the Icon componentstringimage-fill
icon-sizeThe size of the loading icon and the failure iconnumber | string-
icon-prefixIcon class name prefix, equivalent to the class-prefix attribute of the Icon componentstringz-icon
customStylecustom styleobject-

mode valid values

ModeValueDescription
ScalescaleToFillScale the image without maintaining the aspect ratio, so that the width and height of the image are completely stretched to fill the image element
ScaleaspectFitScale the image while maintaining the aspect ratio so that the long side of the image is fully displayed.
ZoomaspectFillScale the image while maintaining the aspect ratio, ensuring that only the short side of the image can be fully displayed.
ScalewidthFixThe width remains unchanged, the height changes automatically, and the aspect ratio of the original image remains unchanged
ScaleheightFixThe height remains unchanged, the width changes automatically, and the aspect ratio of the original image remains unchanged
croptopDo not scale the image, only show the top area of the image
CropbottomDo not scale the image, only show the bottom area of the image
CropcenterDoes not scale the image, only displays the middle area of the image
CropleftDo not scale the image, only display the left area of the image
CroprightDo not scale the image, only show the right area of the image
Croptop leftDo not scale the image, only display the upper left area of the image
Croptop rightDo not scale the image, only display the upper right area of the image
Cropbottom leftDo not scale the image, only display the lower left area of the image
Cropbottom rightDo not scale the image, only display the lower right area of the image

Events

Event nameDescriptionCallback parameters
clickTriggered when the image is clickedevent
loadTriggered when the image is loadedevent
errorTriggered when image loading fails-

Slots

NameDescription
defaultCustomize the content below the image
loadingCustomize loading prompt content
errorCustomize the prompt content when loading fails

Theme customization

Style variables

The component provides the following CSS variables, which can be used to customize styles. For usage, please refer to ConfigProvider component.

NameDefaultDescription
--z-image-placeholder-text-colorvar(--z-text-color-2)-
--z-image-placeholder-font-sizevar(--z-font-size-md)-
--z-image-placeholder-backgroundvar(--z-background)-
--z-image-loading-icon-size64rpx-
--z-image-loading-icon-colorvar(--z-gray-4)-
--z-image-error-icon-size64rpx-
--z-image-error-icon-colorvar(--z-gray-4)-