Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vant Lazyload picture lazy load


May 07, 2021 Vant


Table of contents


Introduced

Lazyload a Vue directive that needs to be registered before it can be used

import Vue from 'vue';
import { Lazyload } from 'vant';

Vue.use(Lazyload);

// 注册时可以配置额外的选项
Vue.use(Lazyload, {
  lazyComponent: true
});

Code demo

Basic usage

Set the v-lazy instruction to the picture that you need to lazy load

<img v-for = "img in imageList" v-lazy = "img" >
export default {
  data() {
    return {
      imageList: [
        'https://img.yzcdn.cn/vant/apple-1.jpg',
        'https://img.yzcdn.cn/vant/apple-2.jpg'
      ]
    };
  }
}

The background map is lazy to load

In addition to picture lazy loading, background lazy loading v-lazy: background-image the value is set to the address of the background picture, it is important to note that the container height must be declared.

<div v-for = "img in imageList" v-lazy:background-image = "img" />

The component is lazy to load

You can do lazy loading by putting components that need to be lazy-loaded in the lazy-component label.

// 注册时设置`lazyComponent`选项
Vue.use(Lazyload, {
  lazyComponent: true
});
<lazy-component>
  <img v-for = "img in imageList" v-lazy = "img" >
</lazy-component>

Api

Options

Parameters Description Type The default
loading The picture at load time string -
error The picture at the time of the error string -
preload The proportion of the preload height string -
attempt The number of attempts number 3
listenEvents The event that was monitored string[] scroll etc
adapter Adapter object -
filter Picture URL filtering object -
lazyComponent Whether the module can be lazy to load boolean false
For more information, please refer to the official vue-lazyload documentation


Example demonstration