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的實現,想要知道更多點擊關注按鈕呦!如果有用請點擊喜歡,謝謝

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 本文作者 Jinkey(微信公眾號 jinkey-love,官網 https://jinkey.ai)原文鏈接 h...
    JinkeyAI閱讀 425,703評論 152 921
  • 我老公是個不愛表達不喜歡承諾的人,跟這種人在一起,真的每天都在自我界限的邊緣游走……今晚約談了一下,又發現了他的一...
    juan子閱讀 152評論 0 0
  • 【近年來,由于成立工作室、參加共同體、開展師徒結對等活動,身邊有了一幫好學上進的年輕人,他們常常讓我回顧來時路,以...
    袁衛星閱讀 1,328評論 8 14
  • 前天打了一下坐,發現背居然可以挺直了,很驚喜,以前怎么也挺不直,看來氣比以前足點了。
    Mary_53cc閱讀 415評論 0 1
  • 佛說善護念無所住而布施,子曰念何可止只是要正。 可見元知識就那么一點,后世各佛各子都是對齊注釋說明...
    三杯水Plus閱讀 444評論 12 3