uni-app H5实现lazyload图片懒加载

使用npm安装 vue-lazyload 组件

npm -i vue-lazyload -S

使用方法 在 components 文件夹下创建一个组件 myImage.vue

<template>
	// 图片方式
	<!-- #ifdef APP-PLUS||MP-WEIXIN --> 
	<image  :lazy-load="true" :src="imgpath" 
	:style='{width:imgwidth,height:imgheight}'
	></image>  
	<!-- #endif -->  
	<!-- #ifdef H5 -->  
	<img  v-lazy="imgpath" >  
	<!-- #endif -->  
</block>  
</template>

<script>
	import Vue from 'vue'
	import VueLazyload from 'vue-lazyload'  
	Vue.use(VueLazyload)
	export default{
		// props:['imgpath']
		props:{
			imgpath:{
				type:String
			},
			imgwidth:{
				type:String,
				default:'100%'
			},
			imgheight:{
				type:String,
				default:'100%'
			}
		}
		
		
	}
</script>

<style>
	img{width: 100%;}
	/* .imgBox{width: 100%;height: 100%;} */
	/* .imageStyle{width: 100%!important;height: 100%!important;;display: block;} */
	img[lazy=loading] {  
	    /*加载中*/  
	}  
	img[lazy=error] {  
	    /*加载失败*/  
	}  
	img[lazy=loaded] {  
	    /*加载完成*/  
	}
</style>

// 背景图方式
		<block v-for="(item,index) in list" :key="index">  
		<!-- #ifdef APP-PLUS -->  
		<image  :lazy-load="true" :src="item.img"></image>  
		<!-- #endif -->  
		<!-- #ifdef H5 -->  
		<view  v-lazy:background-image="{loading:'/static/loading.png',error:'/static/error.png',src:item.img}"></view>  				
		<!-- #endif -->  

在main.js全局中注册

import myImage from './components/myImage.vue'
Vue.component('my-image',myImage)

使用方法

<my-image :imgpath="imgUrl+item.content[0].img"></my-image>
  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值