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 | 小程序 | VUE2 | VUE3 |
|---|---|---|---|---|---|
| √ | √ | √ | √ | √ | √ |
basic use
Configure the width width and height height of the image, and the src path to use.
<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
shapeparameter,circleis a circle, andsquareis a square - If it is a square, you can also set the rounded corner value through the
radiusproperty
<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)
<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.
<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.
<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.
<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
fadeparameter - Configure the transition time of the animation through the
durationparameter, in ms
<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.
<!-- 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 name | description | type | default value | optional values |
|---|---|---|---|---|
| src | Image URL, it is strongly recommended to use absolute or network path | String | - | - |
| mode | Crop mode, see description above | String | aspectFill | - |
| width | Width, the unit is arbitrary, if it is a value, the default unit is px | String / Number | 300 | - |
| height | Height, the unit is arbitrary, if it is a value, the default unit is px | String / Number | 225 | - |
| shape | Image shape, circle, square | String | square | square / square |
| radius | Rounded corners, the default unit is px | String / Number | 0 | - |
| lazyLoad | Whether to lazy load, only for app, h5, WeChat applet | Boolean | true | true / false |
| lazyLoadRootMargin | Lazy 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 view | String / Number | -5 | - |
| minLoadginShowTime | The minimum display time of the loading state (in ms) | String / Number | 50 | - |
| showMenuByLongpress | Whether to open and hold the picture to display the recognition applet code menu, only the WeChat applet is valid | Boolean | true | true / false |
| loadingIcon | Loading icon, or small picture | String | photo | - |
| errorIcon | Icons that failed to load, or small pictures | String | info-circle | - |
| showLoading | Whether to display the loading icon or a custom slot | Boolean | true | true / false |
| showError | Whether to display loading error icon or custom slot | Boolean | true | true / false |
| fade | whether to fade in effect | Boolean | true | true / false |
| webp | Only supports network resources, only valid for WeChat applets | Boolean | false | true / false |
| duration | The transition time with the fade parameter, in ms | String / Number | 500 | - |
| bgColor | Background color, used to blend with the background color when loading images on dark pages | String / Number | #f3f4f6 | - |
Button Slot
| slot name | illustrate |
|---|---|
| loading | Prompt content in custom loading |
| error | Customize the prompt content of loading failure |
Button Events
| event name | illustrate | callback parameters |
|---|---|---|
| click | Triggered when the image is clicked | - |
| error | Triggered when the image fails to load | err: 错误信息 |
| load | Triggered when the image loads successfully | - |
WU-UI