溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

vue-lazyload實現(xiàn)的示例分析

發(fā)布時間:2021-08-02 10:04:53 來源:億速云 閱讀:132 作者:小新 欄目:web開發(fā)

這篇文章給大家分享的是有關(guān)vue-lazyload實現(xiàn)的示例分析的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。

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

然后,在main.js里引入這個模塊。

import 'VueLazyload' from 'vue-lazyload'
Vue.use(VueLazyload,{
   preload:1.3,//預(yù)加載的寬高
   loading:"img的加載中的顯示的圖片的路徑",
   error:"img加載失敗時現(xiàn)實的圖片的路徑",
   attempt:3,//嘗試加載的次數(shù)
   listenEvents:['scroll','wheel','mousewheel','resize','animationend','transitionend','touchmove'], //你想讓vue監(jiān)聽的事件
})

然后在app.vue的template里寫一個

<img v-lazy="img.src"/>

然后在app.vue的script里寫

 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>

感謝各位的閱讀!關(guān)于“vue-lazyload實現(xiàn)的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI