Skip to content

Image Component

Image Component

Image component is used to display images, supporting lazy loading, error handling, preview and other features.

Basic Usage

Basic image component for displaying images, setting width, height and alternative text.

Show CodeArrow
CopyGitHub

Fit Container

Through the fit attribute, you can set how the image adapts to the container.

fill
contain
cover
none
scale-down
Show CodeArrow
CopyGitHub

Image Loading State

The component provides default loading and loading failed states, which can also be customized through slots.

Default Placeholder
Custom Loading
Load Failed
Show CodeArrow
CopyGitHub

Lazy Loading

Setting the lazy attribute enables image lazy loading, which loads only when scrolled into the visible area.

Scroll the container to see lazy loading effect (images load only when scrolled into view)

Image 1
Image 2
Image 3
Image 4
Image 5
Image 6
Show CodeArrow
CopyGitHub

Image Preview

Setting the preview-src-list attribute enables image preview functionality.

Click to View Large Image

Preview Features

  • Click image to view large preview
  • Support ESC key to close preview
  • Support keyboard arrow keys to switch images
  • Support mouse wheel to zoom
  • Support drag to view images
Show CodeArrow
CopyGitHub

Preview Configuration

You can configure preview zoom ratio, initial index, etc.

Image 1
Image 2
Image 3
Show CodeArrow
CopyGitHub

Image Component API

Attributes

AttributeDescriptionTypeAccepted ValuesDefault
srcImage source URLstring
fitHow image fits containerstringfill / contain / cover / none / scale-down
altImage alternative textstring
referrerPolicyImage referrer policystring
lazyWhether lazy loadingbooleanfalse
widthImage widthstring / number
heightImage heightstring / number
previewSrcListPreview image listArray<string>[]
initialIndexPreview initial image indexnumber0
zIndexPreview z-indexnumber2000
infiniteWhether infinite loop previewbooleantrue
closeOnPressEscapeWhether to close preview on ESCbooleantrue
zoomRateZoom ratenumber1.2
minScaleMinimum zoom rationumber0.2
maxScaleMaximum zoom rationumber7

Events

Event NameDescriptionCallback Parameters
loadTriggered when image loads successfully(e: Event)
errorTriggered when image loading fails(e: Error)
switchTriggered when preview image switches(index: number)
closeTriggered when preview closes
showTriggered when preview opens

Slots

Slot NameDescription
placeholderPlaceholder content while image is loading
errorContent when image loading fails

Methods

Method NameDescriptionParameters
showPreviewManually show preview

Style Variables

Image component uses the following style variables, which can be customized through SCSS variables:

Variable NameDescription
$image-placeholder-bg-colorPlaceholder background color
$image-error-bg-colorError state background color
$overlay-bg-colorOverlay background color

Released under the MIT License