easy-loadimage.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <template>
  2. <view class="easy-loadimage" :style="[boxStyle]" :id="uid">
  3. <image
  4. class="origin-img"
  5. :src="imageSrc"
  6. mode="aspectFill"
  7. v-if="loadImg && !isLoadError"
  8. v-show="showImg"
  9. :style="[imgStyle]"
  10. :class="{
  11. 'no-transition': !openTransition,
  12. 'show-transition': showTransition && openTransition,
  13. }"
  14. @load="handleImgLoad"
  15. @error="handleImgError"
  16. >
  17. </image>
  18. <image
  19. class="border-img"
  20. :src="borderSrc"
  21. mode="aspectFill"
  22. v-if="loadImg && !isLoadError && borderSrc"
  23. v-show="showImg"
  24. :style="[imgStyle]"
  25. :class="{
  26. 'no-transition': !openTransition,
  27. 'show-transition': showTransition && openTransition,
  28. }"
  29. >
  30. </image>
  31. <view class="loadfail-img" v-else-if="isLoadError"></view>
  32. <view
  33. :class="['loading-img', 'spin-circle', loadingMode]"
  34. v-show="!showImg && !isLoadError"
  35. ></view>
  36. </view>
  37. </template>
  38. <script>
  39. import { Throttle } from "@/utils/validate.js";
  40. // 生成全局唯一id
  41. function generateUUID() {
  42. return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
  43. let r = (Math.random() * 16) | 0,
  44. v = c == "x" ? r : (r & 0x3) | 0x8;
  45. return v.toString(16);
  46. });
  47. }
  48. export default {
  49. props: {
  50. imageSrc: {
  51. type: String,
  52. default: "",
  53. },
  54. borderSrc: {
  55. type: String,
  56. default: "",
  57. },
  58. mode: {
  59. type: String,
  60. default: "",
  61. },
  62. loadingMode: {
  63. type: String,
  64. default: "looming-gray",
  65. },
  66. openTransition: {
  67. type: Boolean,
  68. default: true,
  69. },
  70. viewHeight: {
  71. type: Number,
  72. default() {
  73. return uni.getWindowInfo().windowHeight;
  74. },
  75. },
  76. width: {
  77. type: String,
  78. default: "",
  79. },
  80. height: {
  81. type: String,
  82. default: "",
  83. },
  84. borderRadius: {
  85. type: String,
  86. default: "",
  87. },
  88. },
  89. data() {
  90. const that = this;
  91. return {
  92. // uid:'',
  93. uid: "uid-" + generateUUID(),
  94. loadImg: false,
  95. showImg: false,
  96. isLoadError: false,
  97. borderLoaded: 0,
  98. showTransition: false,
  99. scrollFn: Throttle(function () {
  100. // 加载img时才执行滚动监听判断是否可加载
  101. if (that.loadImg || that.isLoadError) return;
  102. const id = that.uid;
  103. const query = uni.createSelectorQuery().in(that);
  104. query
  105. .select("#" + id)
  106. .boundingClientRect((data) => {
  107. if (!data) return;
  108. if (data.top - that.viewHeight < 0) {
  109. that.loadImg = !!that.imageSrc;
  110. that.isLoadError = !that.loadImg;
  111. }
  112. })
  113. .exec();
  114. }, 200),
  115. };
  116. },
  117. computed: {
  118. boxStyle() {
  119. return {
  120. width: this.width,
  121. height: this.height,
  122. borderRadius: this.borderRadius,
  123. };
  124. },
  125. imgStyle() {
  126. return {
  127. borderRadius: this.borderRadius,
  128. };
  129. },
  130. },
  131. methods: {
  132. init() {
  133. this.$nextTick(this.onScroll);
  134. },
  135. handleBorderLoad() {
  136. this.borderLoaded = 1;
  137. },
  138. handleBorderError() {
  139. this.borderLoaded = 2;
  140. },
  141. handleImgLoad(e) {
  142. this.showImg = true;
  143. // this.$nextTick(function(){
  144. // this.showTransition = true
  145. // })
  146. setTimeout(() => {
  147. this.showTransition = true;
  148. }, 50);
  149. },
  150. handleImgError(e) {
  151. this.isLoadError = true;
  152. },
  153. onScroll() {
  154. this.scrollFn();
  155. },
  156. },
  157. mounted() {
  158. this.init();
  159. uni.$on("scroll", this.scrollFn);
  160. this.onScroll();
  161. },
  162. beforeDestroy() {
  163. uni.$off("scroll", this.scrollFn);
  164. },
  165. };
  166. </script>
  167. <style scoped>
  168. .easy-loadimage {
  169. position: relative;
  170. }
  171. .border-img {
  172. position: absolute;
  173. width: 100%;
  174. height: 100%;
  175. /* max-height: 360rpx; */
  176. top: 0;
  177. left: 0;
  178. }
  179. /* 官方优化图片tips */
  180. image {
  181. will-change: transform;
  182. }
  183. /* 渐变过渡效果处理 */
  184. image.origin-img {
  185. width: 100%;
  186. height: 100%;
  187. opacity: 0.3;
  188. /* max-height: 360rpx; */
  189. }
  190. image.origin-img.show-transition {
  191. transition: opacity 0.5s;
  192. opacity: 1;
  193. }
  194. image.origin-img.no-transition {
  195. opacity: 1;
  196. }
  197. /* 渐变过渡效果处理 */
  198. image.border-img {
  199. width: 100%;
  200. height: 100%;
  201. opacity: 0.3;
  202. /* max-height: 360rpx; */
  203. }
  204. image.border-img.show-transition {
  205. transition: opacity 0.5s;
  206. opacity: 1;
  207. }
  208. image.border-img.no-transition {
  209. opacity: 1;
  210. }
  211. /* 加载失败、加载中的占位图样式控制 */
  212. .loadfail-img {
  213. height: 100%;
  214. background: url("~@/static/easy-loadimage/loadfail.png") no-repeat center;
  215. background-size: 50%;
  216. }
  217. .loading-img {
  218. height: 100%;
  219. }
  220. /* 转圈 */
  221. .spin-circle {
  222. background: url("~@/static/easy-loadimage/loading.png") no-repeat center;
  223. background-size: 60%;
  224. }
  225. /* 动态灰色若隐若现 */
  226. .looming-gray {
  227. animation: looming-gray 1s infinite linear;
  228. background-color: #e3e3e3;
  229. border-radius: 12rpx;
  230. }
  231. @keyframes looming-gray {
  232. 0% {
  233. background-color: #e3e3e3aa;
  234. }
  235. 50% {
  236. background-color: #e3e3e3;
  237. }
  238. 100% {
  239. background-color: #e3e3e3aa;
  240. }
  241. }
  242. /* 骨架屏1 */
  243. .skeleton-1 {
  244. background-color: #e3e3e3;
  245. background-image: linear-gradient(
  246. 100deg,
  247. rgba(255, 255, 255, 0),
  248. rgba(255, 255, 255, 0.2) 50%,
  249. rgba(255, 255, 255, 0) 80%
  250. );
  251. background-size: 100rpx 100%;
  252. background-repeat: repeat-y;
  253. background-position: 0 0;
  254. animation: skeleton-1 0.6s infinite;
  255. }
  256. @keyframes skeleton-1 {
  257. to {
  258. background-position: 200% 0;
  259. }
  260. }
  261. /* 骨架屏2 */
  262. .skeleton-2 {
  263. background-image: linear-gradient(
  264. -90deg,
  265. #fefefe 0%,
  266. #e6e6e6 50%,
  267. #fefefe 100%
  268. );
  269. background-size: 400% 400%;
  270. background-position: 0 0;
  271. animation: skeleton-2 1.2s ease-in-out infinite;
  272. }
  273. @keyframes skeleton-2 {
  274. to {
  275. background-position: -135% 0;
  276. }
  277. }
  278. </style>