Home > Article > Web Front-end > Use uniapp to achieve lazy loading image effect
Use uniapp to achieve lazy loading image effect
With the development of mobile Internet, pictures play an important role in mobile applications. However, loading too many images may cause the page to load slowly and affect the user experience. In order to solve this problem, we can use the lazy loading image effect provided by the uniapp framework, so that the image is loaded only when needed, saving page loading time and improving user experience.
First of all, we need to ensure that the uniapp development environment has been installed and a uniapp project has been created. Next, we'll start writing the code.
uni-image
component provided by uniapp to achieve this. The code is as follows: <template> <uni-image src="{{ showImage ? imageUrl : placeholderUrl }}" @load="handleLoad" @error="handleError" ></uni-image> </template>
In the above code, we use double curly braces {{}}
to bind the src attribute of the image. According to the condition, when showImage
is true, the value of imageUrl
is displayed as the image address; when showImage
is false, placeholderUrl
is displayed. The value is used as the placeholder image address.
showImage
and imageUrl
. showImage
is used to control whether to display images, imageUrl
is used to store the image address. In the created
life cycle function of the component, we can initialize the values of showImage
and imageUrl
. We can initialize showImage
to false, which means the image will not be displayed. imageUrl
can be obtained through the props attribute. When the component is used, the image address is passed through the attribute. The code is as follows:
<script> export default { props: { url: { type: String, required: true }, placeholder: { type: String, default: 'placeholder.jpg' }, }, data() { return { showImage: false, imageUrl: '' } }, created() { this.imageUrl = this.url; }, methods: { handleLoad() { this.showImage = true; }, handleError() { this.imageUrl = this.placeholder; } } } </script>
In the above code, we defined two methods handleLoad
and handleError
to handle the event of image loading completion and loading failure. . When the image loads successfully, we set the value of showImage
to true and the image will be displayed on the page. When the image fails to load, we set the value of imageUrl
to the address of the placeholder image to ensure that there is always an image displayed on the page.
In pages that require lazy loading of images, you first need to import the component. In the script tag of the page, add the following code:
import LazeImage from '@/components/LazeImage.vue'
Then in the template tag of the page, use the <laze-image></laze-image>
tag to introduce the "LazeImage" component. At the same time, we need to add an attribute url
to the <laze-image></laze-image>
tag to pass the image address. The code is as follows:
<template> <view> <laze-image :url="imageSrc"></laze-image> </view> </template>
In the above code, we use the v-for
directive to render multiple images in a loop. imageSrc
is an array that stores the addresses of multiple images.
placeholderUrl
as the address of the placeholder image. We need to place the placeholder image in the static
folder of the project and configure its path to the placeholder
attribute of the component. The code is as follows: <laze-image :url="imageSrc" placeholder="static/placeholder.jpg"></laze-image>
Through the above steps, we have successfully used uniapp to achieve the lazy loading image effect. When the page loads, the images will not be loaded immediately, but will be loaded when needed, saving page loading time and improving user experience.
It should be noted that in actual projects, we may expand the lazy loading image effect according to specific needs, such as loading images only when they appear in the visible area. The above code is only a basic implementation and can be expanded and optimized according to actual project needs.
The above is the detailed content of Use uniapp to achieve lazy loading image effect. For more information, please follow other related articles on the PHP Chinese website!