Home > Web Front-end > uni-app > body text

Use uniapp to achieve lazy loading image effect

PHPz
Release: 2023-11-21 18:15:41
Original
1300 people have browsed it

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.

  1. Create a Vue component
    In the uniapp project, we can create an independent Vue component to achieve the lazy loading image effect. Open the uniapp project, enter the components folder, and create a component named "LazeImage".
  2. Writing component template
    In the "LazeImage" component, we need to write a template to display the image. We can use the 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>
Copy after login

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.

  1. Writing component logic
    Next step, we need to write the logic code of the component. In the script tag of the "LazeImage" component, we define two data data: 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>
Copy after login

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.

  1. Using components
    Now, we have completed the development of the "LazeImage" component. We can use it in other pages to achieve the effect of lazy loading of images.

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'
Copy after login

Then in the template tag of the page, use the <laze-image> tag to introduce the "LazeImage" component. At the same time, we need to add an attribute url to the <laze-image> tag to pass the image address. The code is as follows:

<template>
  <view>
    <laze-image :url="imageSrc"></laze-image>
  </view>
</template>
Copy after login

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.

  1. Configure placeholder image
    Remember that in the "LazeImage" component, we used 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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template