vue-lazyload的详细过程

首先 ,在命令行输入npm install vue-lazyload&&cnpm install vue-lazyload

然后,在main.js里引入这个模块。

import 'VueLazyload' from 'vue-lazyload'
Vue.use(VueLazyload,{
      preload:1.3,//预加载的宽高
      loading:"img的加载中的显示的图片的路径",
      error:"img加载失败时现实的图片的路径",
      attempt:3,//尝试加载的次数
      listenEvents:['scroll','wheel','mousewheel','resize','animationend','transitionend','touchmove'], //你想让vue监听的事件
})

然后在app.vuetemplate里写一个
<img v-lazy="img.src"/>
然后在app.vuescript里写

  data(){
    return {
      img:{
          src:"图片的真是路径"
                }
            }
       }

捋一下思路:

//main.js




// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import $ from 'jquery'
import 'assets/bootstrap/css/bootstrap.min.css'
import 'assets/bootstrap/js/bootstrap.min'
import router from '@/router/index'
import VueLazyload from 'vue-lazyload'
Vue.use(VueLazyload,{
    preload:1.3,
    loading:require('../static/imgs/ad3.png'),
//解释一下为什么是require('.....url'):因为vue自带webpack打包工具,如果是图片路径就会把他当成模块解析,所以直接引入就好了。
//记得把里面的路径换成自己的哦
    listenEvents:['mousewheel'],
})
//载入vue-router
//import Vue from 'vue'
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  template: '<App/>',
  components: { App }
})
//app.vue




<template>
  <div id="app">
    <navbar></navbar>
    <router-view></router-view>
   <hello></hello>
  <ul>
    <li v-for="item in imgUrl">
      <img v-lazy="item.src" alt="" width="300" height="150"/>
    </li>
  </ul>
  <img v-lazy='img[0].src'/>
  </div>
</template>

<script>
    import hello from './components/Hello'
    import Navbar from '@/components/navBar'
    import route from '@/components/route'
export default {
  name: 'app',
  components:{
    hello,
    Navbar
  },
  data() {
    return {
      imgUrl: [
        {src: require('@/assets/imgs/ad1.png')},//记得把里面的路径换成自己的哦
       {src: require('@/assets/imgs/ad1.png')},//记得把里面的路径换成自己的哦
      ],
      img:[
        {src:require('@/assets/imgs/ad2.png')}//记得把里面的路径换成自己的哦
      ]
    }
  }
}
</script>

<style>
#app {
  font-family: 'Avenir', Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
} 
</style>


这只是一个简单的vue-lazyload的实现,想要知道更多点击关注按钮呦!如果有用请点击喜欢,谢谢

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 本文作者 Jinkey(微信公众号 jinkey-love,官网 https://jinkey.ai)原文链接 h...
    JinkeyAI阅读 425,902评论 152 921
  • 我老公是个不爱表达不喜欢承诺的人,跟这种人在一起,真的每天都在自我界限的边缘游走……今晚约谈了一下,又发现了他的一...
    juan子阅读 1,310评论 0 0
  • 【近年来,由于成立工作室、参加共同体、开展师徒结对等活动,身边有了一帮好学上进的年轻人,他们常常让我回顾来时路,以...
    袁卫星阅读 5,098评论 8 14
  • 前天打了一下坐,发现背居然可以挺直了,很惊喜,以前怎么也挺不直,看来气比以前足点了。
    Mary_53cc阅读 3,093评论 0 1
  • 佛说善护念无所住而布施,子曰念何可止只是要正。 可见元知识就那么一点,后世各佛各子都是对齐注释说明...
    三杯水Plus阅读 3,188评论 12 3

友情链接更多精彩内容