HomeDocs
Skip to main content

Image

Introduction

The Image component is used to display images in various formats such as JPG, PNG, SVG, WEBP, GIF, etc.

Attribute Description

AttributeTypeDefault ValueRequiredDescriptionMinimum Version
srcStringN/AYesThe URL of the image resource
data-srcStringN/ANoThe URL of the image resource for lazy loading
modeStringscaleToFillNoThe mode for image scaling and cropping
lazyLoadBooleanfalseNoWhether to enable image lazy loading> 0.1.0
binderrorEventHandleN/ANoThe event name published to AppService when an error occurs
bindloadEventHandleN/ANoThe event name published to AppService when the image is loaded

Valid values for mode

There are 13 modes available, including 4 scaling modes and 9 cropping modes.

ModeValueDescriptionMinimum Version
ScalingscaleToFillScale the image without maintaining the aspect ratio to completely stretch the width and height to fit the image element
ScalingaspectFitScale the image while maintaining the aspect ratio so that the long side of the image is fully displayed
ScalingaspectFillScale the image while maintaining the aspect ratio so that the short side of the image is fully displayed
ScalingheightFixKeep the height unchanged and automatically adjust the width to maintain the original aspect ratio
ScalingwidthFixKeep the width unchanged and automatically adjust the height to maintain the original aspect ratio
CroppingtopDo not scale the image and only display the top area of the image
CroppingbottomDo not scale the image and only display the bottom area of the image
CroppingcenterDo not scale the image and only display the center area of the image
CroppingleftDo not scale the image and only display the left area of the image
CroppingrightDo not scale the image and only display the right area of the image
Croppingtop leftDo not scale the image and only display the top left area of the image
Croppingtop rightDo not scale the image and only display the top right area of the image
Croppingbottom leftDo not scale the image and only display the bottom left area of the image
Croppingbottom rightDo not scale the image and only display the bottom right area of the image

Examples

Code Example 1: Custom Scaling Modes

<view class="wrap">
<view class="card-area" s-for="item in scaleArray">
<image
:class="['image-area', item.hasBackgroud == 1 ? 'backGround': '']"
:data-name="item.mode"
:mode="item.mode"
:src="src"
:key="item.mode"
bind:tap="onTap"
bind:binderror="imageError"
bind:bindload="imageLoad"
/>
<view class="bottom-description">{{ item.text }}</view>
</view>
</view>
Page({
data: {
scaleArray: [
{
mode: 'scaleToFill',
text: 'scaleToFill: Stretch the image to fit the image element without maintaining the aspect ratio',
},
{
mode: 'aspectFit',
text: 'aspectFit: Scale the image while maintaining the aspect ratio so that the long side is fully displayed',
hasBackgroud: 1,
},
{
mode: 'aspectFill',
text: 'aspectFill: Scale the image while maintaining the aspect ratio so that the short side is fully displayed',
},
{
mode: 'widthFix',
text: 'widthFix: Keep the width unchanged and automatically adjust the height to maintain the aspect ratio',
},
{
mode: 'heightFix',
text: 'heightFix: Keep the height unchanged and automatically adjust the width to maintain the aspect ratio',
},
],
src: 'https://b.bdstatic.com/miniapp/images/demo-dog.png',
},
imageError(e) {
console.log('Image error occurred', e.detail.errMsg);
},
onTap(e) {
console.log('Image tapped', e);
},
imageLoad(e) {
console.log('Image loaded successfully', e.type);
},
});

Code Example 2: Custom Cropping Modes

<view class="wrap">
<view class="card-area" s-for="item in cutArray">
<image
class="image-area"
:data-name="item.mode"
:lazy-load="true"
:image-menu-prevent="true"
:mode="item.mode"
:src="src"
:key="item.mode"
bind:tap="onTap"
bind:binderror="imageError"
bind:bindload="imageLoad"
/>
<view class="bottom-description">{{item.text}}</view>
</view>
</view>
Page({
data: {
cutArray: [
{
mode: 'top',
text: 'top: Do not scale the image and only display the top area',
},
{
mode: 'bottom',
text: 'bottom: Do not scale the image and only display the bottom area',
},
{
mode: 'center',
text: 'center: Do not scale the image and only display the center area',
},
{
mode: 'left',
text: 'left: Do not scale the image and only display the left area',
},
{
mode: 'right',
text: 'right: Do not scale the image and only display the right area',
},
{
mode: 'top left',
text: 'top left: Do not scale the image and only display the top left area',
},
{
mode: 'top right',
text: 'top right: Do not scale the image and only display the top right area',
},
{
mode: 'bottom left',
text: 'bottom left: Do not scale the image and only display the bottom left area',
},
{
mode: 'bottom right',
text: 'bottom right: Do not scale the image and only display the bottom right area',
},
],
src: 'https://b.bdstatic.com/miniapp/images/demo-dog.png',
},
imageError(e) {
console.log('Image error occurred', e.detail.errMsg);
},
onTap(e) {
console.log('Image tapped', e);
},
imageLoad(e) {
console.log('Image loaded successfully', e.type);
},
});

Code Example 3: Image Lazy Loading

<view class="img-wrap">
<!-- When lazy loading is enabled, use data-src for the actual image source and src for the placeholder image -->
<image
class="demo-img"
src="/images/img_5222.png"
data-src="/images/img_5223.png"
:lazy-load="true"
/>
</view>
Privacy agreementDeveloper agreementcontact us: developer_service.mi@transsion.com © 2024 MiniApp. All Rights Reserved.