明霞山资源网 Design By www.htccd.com
本文介绍了浅谈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.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>
这只是一个简单的vue-lazyload的实现,希望对大家的学习有所帮助,也希望大家多多支持。
明霞山资源网 Design By www.htccd.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
明霞山资源网 Design By www.htccd.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。