Home  >  Article  >  Web Front-end  >  Introduction to the Vue-based lazy loading plug-in vue-view-lazy

Introduction to the Vue-based lazy loading plug-in vue-view-lazy

不言
不言Original
2018-07-04 11:54:181907browse

This article mainly introduces the lazy loading plug-in vue-view-lazy based on Vue, which can load pictures or other resources after entering the visible area. The content is quite good. I will share it with you now and give it as a reference. .

Lazy loading plug-in based on vue

Purpose: Load pictures or other resources after entering the visible area

Install and use

  1. Directly download vue-view-lazy.min.js in the dist directory and use

  2. Install using npm

Use directly

<p id="app">
  <span v-view-lazy @model="handleModel"></span>
</p>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
<script src="./dist/vue-view-lazy.min.js"></script>
<script>
  Vue.use(vViewLazy.default,{});
  new Vue({
    el:&#39;#app&#39;,
    data:{
      msg:&#39;数据&#39;
    },
    methods:{
      handleModel(){
        console.log(&#39;出现了&#39;);
      },
    },
  })
</script>

npm:

$ npm install --save-dev vue-view-lazy

Introducing vue-view-lazy

.main file

import vView from &#39;vue-view-lazy&#39;
Vue.use(vView,{
  error:&#39;../../static/images/loading.png&#39;,
  loading:&#39;../../static/images/loading.gif&#39;,
});

Lazy loading of pictures

.vue file

<template>
  <ul id=&#39;img&#39;>
    <li class="in" v-for="(item,i) in imgs" :key="i">
      <img src="#" alt="图片" v-view-lazy="item.src">
    </li>
  </ul>
</template>

<script>
  export default {
    data () {
      return {
        msg: &#39;Welcome to Your Vue.js App&#39;,
        imgs:[
          {src:&#39;../../static/images/img1.jpg&#39;},
          {src:&#39;../../static/images/img2.png&#39;},
          {src:&#39;../../static/images/img2.jpg&#39;},
          {src:&#39;../../static/images/img3.jpg&#39;},
          {src:&#39;../../static/images/img4.jpg&#39;},
          {src:&#39;../../static/images/img5.jpeg&#39;}, 
        ]
      }
    },
    mounted(){
    },
  }
</script>
<style scoped>
  ...
</style>

Lazy loading data

.vue file

<template>
  <p>
    <!--@model自定义事件是在该dom在第一次出现在视口内时触发的方法-->
    <!--v-view-lazy=&#39;method&#39; 或 v-view-lazy=&#39;(e)=>method(e,...arg)&#39;-->
    <p class="cnt" v-for="(v,i) in msg" :key="i" v-view-lazy @model="(e)=>getAjaxContent(e,v.msg)">
      loading...
    </p>
    <p class="cnt" v-for="(v,i) in msg" :key="i" v-view-lazy @model="getAjaxContent()">
      loading...
    </p>
  </p>
</template>

<script>
  export default {
    data(){
      return{
        msg:[]
      }
    },
    mounted(){
      fetch(&#39;http://localhost:3000/test&#39;).then(res=>res.json()).then(res=>{
        this.msg = res;
      })
    },
    methods:{
      getAjaxContent(event,msg){
        event.innerText = msg
      },
    }
  }
</script>

<style scoped>
  .cnt {
    /*background: #ececec;*/
    height: 500px;
    margin-bottom: 50px;
  }
</style>

The above is the entire content of this article, I hope it will be helpful Everyone’s learning is helpful. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

Method to implement button-level permission control based on Vue custom instructions

Introduction to vue component name

The above is the detailed content of Introduction to the Vue-based lazy loading plug-in vue-view-lazy. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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