Skip to content
🌏 Translated with the assistance of DeepSeek and ChatGPT

Image

As the name suggests, displays an image.

The example images in this document are from Lorem Picsum.

Basic Usage

You can set src, srcset, and alt, and use it just like the <img> tag.

Image
<template>
	<px-image
		src="https://picsum.photos/id/106/200/100"
		srcset="https://picsum.photos/id/106/400/200 2x, https://picsum.photos/id/106/600/300 3x"
		alt="Image"
		style="width: 200px; height: 100px"
	></px-image>
</template>

Fit to Container

The objectFit property sets how the image fits its container, similar to the CSS object-fit.

Image
Image
Image
Image
Image
<template>
	<px-space>
		<px-image
			v-for="value in objectFit"
			src="https://picsum.photos/id/106/200/100"
			alt="Image"
			:object-fit="value"
			style="width: 100px; height: 100px"
		></px-image>
	</px-space>
</template>

<script setup lang="ts">
const objectFit = ['fill', 'contain', 'cover', 'none', 'scale-down']
</script>

Placeholder and Load Failure

placeholder and error respectively set the content displayed while loading and when loading fails.

Image
Loading...
Image
<template>
	<px-space style="font-size: 0">
		<px-image
			src="https://picsum.photos/id/106/200/100"
			alt="Image"
			style="width: 200px; height: 100px"
		>
			<template #placeholder> Loading... </template>
		</px-image>
		<px-image src="error-url" alt="Image" style="width: 200px; height: 100px">
			<template #error> Error! </template>
		</px-image>
	</px-space>
</template>

Lazy Loading

The loading property enables native browser image lazy loading. lazy enables lazy loading based on IntersectionObserver, and the root property sets the visible area for lazy loading.

To prevent reflow and reduce page jitter, it is recommended to set the image size when using lazy loading.

Loading Prop

Image
Image
Image
Image
Image
Image
Image
Image
Image
Image

Lazy Prop

<template>
	<px-space>
		<div>
			<h4>Loading Prop</h4>
			<div class="lazy-container-left">
				<px-image
					v-for="value in 10"
					:key="value"
					src="https://picsum.photos/id/106/200/100"
					alt="Image"
					style="width: 100%; height: 100px"
					loading="lazy"
				></px-image>
			</div>
		</div>
		<div>
			<h4>Lazy Prop</h4>
			<div class="lazy-container-right">
				<px-image
					v-for="value in 10"
					:key="value"
					src="https://picsum.photos/id/106/200/100"
					alt="Image"
					style="width: 100%; height: 100px"
					lazy
					root=".lazy-container-righ"
				></px-image>
			</div>
		</div>
	</px-space>
</template>

<style lang="css" scoped>
.lazy-container-left,
.lazy-container-right {
	width: 200px;
	height: 500px;
	font-size: 0;
	overflow: auto;
}
</style>

Preview

Enable large image preview with previewable. The preview image size is calculated based on the image resource dimensions.

Image
<template>
	<px-image
		src="https://picsum.photos/id/106/600/300"
		alt="Image"
		previewable
		style="width: 200px; height: 100px"
	></px-image>
</template>

API

ImageProps

AttributeTypeOptionalDefaultDescriptionVersion
srcstringTrueImage resource URL, behaves like the native <img> src.0.0.3
srcsetstringTrueResponsive image resource URL, behaves like the native <img> srcset.0.0.3
altstringTrueSame as the native <img> alt.0.0.3
objectFit'fill' | 'contain' | 'cover' | 'none' | 'scale-down'True'fill'How the image fits its container, same as CSS object-fit.0.0.3
loading'eager' | 'lazy'True'eager'Native image lazy loading.0.0.3
lazybooleanTruefalseImage lazy loading. When enabled, the loading property will be disabled. This property is not reactive.0.0.3
rootHTMLElement | stringTrueFor image lazy loading based on the lazy property, sets the visible area element. If empty, defaults to the viewport. This property is not reactive.0.0.3
rootMarginnumber | [number, number]True[100, 200]For image lazy loading based on the lazy property, sets the distance from the visible area. This property is not reactive.0.0.3
previewablebooleanTruefalseWhether to enable large image preview.0.0.3
maskPropsOmit<MaskProps, 'zIndex'>TrueProperties for the mask during preview.0.0.3
popupWrapperProps{ zIndex: number, root: HTMLElement | string }TrueProperties for the floating layer during preview.0.0.3
popupWrapperProps{ maxWidth?: number, maxHeight?: number, margin?: number}True{ margin: 32, maxWidth: Infinity, maxHeight: Infinity }Properties for the floating layer during preview.0.0.3
referrerpolicystringTrue'no-referrer'Same as the native <img> referrerpolicy.0.0.3
crossorigin'anonymous' | 'use-credentials' | ''True''Same as the native <img> crossorigin.0.0.3

ImageEvents

EventParameterDescriptionVersion
loadimg: HTMLImageElement, event: EventCallback when the image loads successfully.0.0.3
errorimg: HTMLImageElement, error: string | EventCallback when the image fails to load.0.0.3
loadingimg: HTMLImageElementCallback when the image starts loading with the lazy property.0.0.3
previewevent: MouseEventCallback when previewing the image.0.0.3
closeevent: MouseEventCallback when closing the image preview.0.0.3

ImageSlots

SlotParameterDescriptionVersion
placeholderPlaceholder element while loading.0.0.3
errorContent displayed when loading fails.0.0.3