Skip to content

Image

This component is an optimized image component based on uniapp, which supports lazy loading of images, preloading, post-loading effects, placeholders for loading and loading failures, etc.

Solved a major pain point: the problem of lazy loading not working, and manually supported app, weixin, h5.

About lazy loading (lazy loading is used by default)

Since the example uses the same image, even if the lazy loading list is very long, the cache of the existing image loads quickly, and the effect of lazy loading may not be visible.

Therefore, we set the minLoadginShowTime attribute to specify the minimum display time of the loading state (default 50, unit ms), so that you can better view the effect of lazy loading (we recommend you to scan the QR code above to view the effect directly)

It is recommended that you set minLoadginShowTime to 0 when using the online project to optimize performance.

platform compatibility

App(vue)App(nvue)H5小程序VUE2VUE3

basic use

Configure the width width and height height of the image, and the src path to use.

vue
<template>
  <wu-image
    :show-loading="true"
    :src="src"
    width="80px"
    height="80px"
    @click="click"
  ></wu-image>
</template>

<script>
export default {
  data() {
    return {
      src: "https://wuui.cn/1.jpeg",
    };
  },
};
</script>
<template>
  <wu-image
    :show-loading="true"
    :src="src"
    width="80px"
    height="80px"
    @click="click"
  ></wu-image>
</template>

<script>
export default {
  data() {
    return {
      src: "https://wuui.cn/1.jpeg",
    };
  },
};
</script>

picture shape

  • Set the shape of the image through the shape parameter, circle is a circle, and square is a square
  • If it is a square, you can also set the rounded corner value through the radius property
vue
<wu-image src="https://wuui.cn/1.jpeg" shape="circle"></wu-image>
<wu-image src="https://wuui.cn/1.jpeg" shape="circle"></wu-image>

clipping mode

Configure the filling mode through the mode parameter, which is exactly the same as the mode parameter of the image component of uni-app, see: [image](https://uniapp.dcloud.net.cn/component/ image.html#mode-valid value)

vue
<wu-image src="https://wuui.cn/1.jpeg" mode="widthFix"></wu-image>
<wu-image src="https://wuui.cn/1.jpeg" mode="widthFix"></wu-image>

Lazy loading

**Note: Only for app, h5, and WeChat applets, it is enabled by default. **

lazy-load is used to enable or disable lazy loading

lazy-load-root-margin is used to set the root margin of lazy loading. If you set a negative number, it will be displayed after entering the specified distance from the view. If you set a positive number, it will be displayed when it is far away from the view.

vue
<wu-image
  v-for="num in 30"
  :key="num"
  src="https://wuui.cn/1.jpeg"
  width="80px"
  height="80px"
  :lazy-load="true"
  :lazy-load-root-margin="-5"
></wu-image>
<wu-image
  v-for="num in 30"
  :key="num"
  src="https://wuui.cn/1.jpeg"
  width="80px"
  height="80px"
  :lazy-load="true"
  :lazy-load-root-margin="-5"
></wu-image>

loading prompt

During the image loading process, it is in the loading state (a small icon is displayed by default), you can use the loading custom slot, combined with the wu-loading component of wu-ui, to achieve the loading animation effect.

html
<wu-image src="https://wuui.cn/1.jpeg">
  <template v-slot:loading>
    <wu-loading-icon color="red"></wu-loading-icon>
  </template>
</wu-image>
<wu-image src="https://wuui.cn/1.jpeg">
  <template v-slot:loading>
    <wu-loading-icon color="red"></wu-loading-icon>
  </template>
</wu-image>

loading error message

When the picture fails to load, an error prompt icon is displayed by default. You can customize the slot through error to realize a personalized prompt method.

html
<wu-image src="https://wuui.cn/1.jpeg">
  <template v-slot:error>
    <view style="font-size: 24rpx;">Loading Error</view>
  </template>
</wu-image>
<wu-image src="https://wuui.cn/1.jpeg">
  <template v-slot:error>
    <view style="font-size: 24rpx;">Loading Error</view>
  </template>
</wu-image>

fade animation

The component comes with a fade-in animation effect when loading is complete:

  • Configure whether to enable the animation effect through the fade parameter
  • Configure the transition time of the animation through the duration parameter, in ms
vue
<wu-image src="https://wuui.cn/1.jpeg" :fade="true" duration="450"></wu-image>
<wu-image src="https://wuui.cn/1.jpeg" :fade="true" duration="450"></wu-image>

event bubbling

By default, the component allows internal and external event bubbling, because in many cases, we want to click on the picture, and the click event of the parent element where the picture is located can also be triggered.

If you want to avoid event bubbling, you can nest a view outside the component and add @tap.stop to it.

html
<!-- Clicking on the image will not trigger the clickHandler -->
<view @tap="clickHandler">
	<view @tap.stop>
		<wu-image src="https://wuui.cn/1.jpeg"></wu-image>
	</view>
</view>
<!-- Clicking on the image will not trigger the clickHandler -->
<view @tap="clickHandler">
	<view @tap.stop>
		<wu-image src="https://wuui.cn/1.jpeg"></wu-image>
	</view>
</view>

API

Button Props

property namedescriptiontypedefault valueoptional values ​​
srcImage URL, it is strongly recommended to use absolute or network pathString--
modeCrop mode, see description aboveStringaspectFill-
widthWidth, the unit is arbitrary, if it is a value, the default unit is pxString / Number300-
heightHeight, the unit is arbitrary, if it is a value, the default unit is pxString / Number225-
shapeImage shape, circle, squareStringsquaresquare / square
radiusRounded corners, the default unit is pxString / Number0-
lazyLoadWhether to lazy load, only for app, h5, WeChat appletBooleantruetrue / false
lazyLoadRootMarginLazy loading root margin sets the negative number to display after the specified distance from the view, and displays when the positive number is far from the viewString / Number-5-
minLoadginShowTimeThe minimum display time of the loading state (in ms)String / Number50-
showMenuByLongpressWhether to open and hold the picture to display the recognition applet code menu, only the WeChat applet is validBooleantruetrue / false
loadingIconLoading icon, or small pictureStringphoto-
errorIconIcons that failed to load, or small picturesStringinfo-circle-
showLoadingWhether to display the loading icon or a custom slotBooleantruetrue / false
showErrorWhether to display loading error icon or custom slotBooleantruetrue / false
fadewhether to fade in effectBooleantruetrue / false
webpOnly supports network resources, only valid for WeChat appletsBooleanfalsetrue / false
durationThe transition time with the fade parameter, in msString / Number500-
bgColorBackground color, used to blend with the background color when loading images on dark pagesString / Number#f3f4f6-

Button Slot

slot nameillustrate
loadingPrompt content in custom loading
errorCustomize the prompt content of loading failure

Button Events

event nameillustratecallback parameters
clickTriggered when the image is clicked-
errorTriggered when the image fails to loaderr: 错误信息
loadTriggered when the image loads successfully-