Blame view
src/components/EasyLoadimage/EasyLoadimage.vue
4.61 KB
93e6a2650 首页懒加载 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
<template> <view class="easy-loadimage" :id="uid"> <image class="origin-img" :src="imageSrc?imageSrc:defaultImg" :mode="mode" v-if="loadImg&&!isLoadError" v-show="showImg" :class="{'no-transition':!openTransition,'show-transition':showTransition&&openTransition}" @load="handleImgLoad" @error="handleImgError"> </image> <view class="loadfail-img" v-else-if="isLoadError"></view> <view :class="['loading-img',loadingMode]" v-show="!showImg&&!isLoadError"></view> </view> </template> <script> // 生成全局唯一id function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { let r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); return v.toString(16); }) } export default{ props:{ imageSrc:{ type: String, }, mode:{ type: String, }, scrollTop:{ type: Number, }, loadingMode:{ type: String, default:'looming-gray' }, openTransition:{ type: Boolean, default:true, }, viewHeight:{ type:Number, default() { return uni.getSystemInfoSync().windowHeight; } } }, watch:{ scrollTop(val){ this.onScroll(val) } }, data(){ return { uid:'', loadImg:false, showImg:false, isLoadError:false, showTransition:false } }, methods:{ init(){ this.uid = 'uid-' + generateUUID(); this.$nextTick(this.onScroll) }, handleImgLoad(e){ // console.log('success'); this.showImg = true; // this.$nextTick(function(){ // this.showTransition = true // }) setTimeout(()=>{ this.showTransition = true },50) }, handleImgError(e){ // console.log('fail'); this.isLoadError = true; }, onScroll(scrollTop){ // 加载ing时才执行滚动监听判断是否可加载 if(this.loadImg || this.isLoadError) return; const id = this.uid const query = uni.createSelectorQuery().in(this); query.select('#'+id).boundingClientRect(data => { if(!data) return; if(data.top - this.viewHeight<0){ this.loadImg = true; } }).exec() }, }, mounted() { this.init() } } </script> <style scoped> /* 官方优化图片tips */ image{ will-change: transform } /* 渐变过渡效果处理 */ image.origin-img{ width: 100%; height: 100%; opacity: 0.3; } image.origin-img.show-transition{ transition: opacity 1.2s; opacity: 1; } image.origin-img.no-transition{ opacity: 1; } /* 加载失败、加载中的占位图样式控制 */ .loadfail-img{ height: 100%; background: url('~@/static/easy-loadimage/loadfail.png') no-repeat center; background-size: 50%; } .loading-img{ height: 100%; } /* 转圈 */ .spin-circle{ background: url('~@/static/easy-loadimage/loading.gif') no-repeat center; background-size: 100rpx; } /* 动态灰色若隐若现 */ .looming-gray{ animation: looming-gray 1s infinite linear; background-color: #e3e3e3; } @keyframes looming-gray{ 0% {background-color:#e3e3e3aa;} 50% {background-color:#e3e3e3;} 100% {background-color:#e3e3e3aa;} } /* 骨架屏1 */ .skeleton-1{ background-color: #e3e3e3; background-image: linear-gradient(100deg, rgba(255, 255, 255, 0), rgba(255, 255, 255, 0.2) 50%, rgba(255, 255, 255, 0) 80%); background-size: 100rpx 100%; background-repeat: repeat-y; background-position:0 0; animation: skeleton-1 .6s infinite; } @keyframes skeleton-1 { to { background-position: 200% 0; } } /* 骨架屏2 */ .skeleton-2{ background-image: linear-gradient(-90deg, #fefefe 0%, #e6e6e6 50%,#fefefe 100%); background-size: 400% 400%; background-position:0 0; animation: skeleton-2 1.2s ease-in-out infinite; } @keyframes skeleton-2{ to { background-position: -135% 0; } } </style> |