淺談vue-lazyload實現(xiàn)的詳細(xì)過程
本文介紹了淺談vue-lazyload實現(xiàn)的詳細(xì)過程,分享給大家,也給自己留個筆記
首先 ,在命令行輸入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打包工具,如果是圖片路徑就會把他當(dāng)成模塊解析,所以直接引入就好了。
//記得把里面的路徑換成自己的哦
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的實現(xiàn),希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue?el-switch初始值(默認(rèn)值)不能正確顯示狀態(tài)問題及解決
這篇文章主要介紹了vue?el-switch初始值(默認(rèn)值)不能正確顯示狀態(tài)問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10
vue2.0 實現(xiàn)導(dǎo)航守衛(wèi)(路由守衛(wèi))
vue-route 提供的 beforeRouteUpdate 可以方便地實現(xiàn)導(dǎo)航守衛(wèi)(navigation-guards)。這篇文章主要介紹了vue2.0 實現(xiàn)導(dǎo)航守衛(wèi)(路由守衛(wèi))的相關(guān)知識,需要的朋友可以參考下2018-05-05
使用Vue+Django+Ant Design做一個留言評論模塊的示例代碼
這篇文章主要介紹了使用Vue+Django+Ant Design做一個留言評論模塊,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06
Vue?2源碼閱讀?Provide?Inject?依賴注入詳解
這篇文章主要為大家介紹了Vue?2源碼閱讀?Provide?Inject?依賴注入詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08

