index.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <template>
  2. <view v-if="loading" :style="{
  3. width: windowWinth + 'px',
  4. height: windowHeight + 'px',
  5. backgroundColor: bgColor,
  6. position: 'absolute',
  7. left: left + 'px',
  8. top: top + 'px',
  9. zIndex: 100,
  10. overflow: 'hidden'
  11. }"
  12. @touchmove.stop.prevent>
  13. <view v-for="(item, index) in RectNodes" :key="$u.guid()" :class="[animation ? 'skeleton-fade' : '']" :style="{
  14. width: item.width + 'px',
  15. height: item.height + 'px',
  16. backgroundColor: elColor,
  17. position: 'absolute',
  18. left: (item.left - left) + 'px',
  19. top:(item.top - top)<0?(item.top - top +windowHeight-80)+ 'px':(item.top - top) + 'px'
  20. }"></view>
  21. <view v-for="(item, index) in circleNodes" :key="$u.guid()" :class="animation ? 'skeleton-fade' : ''" :style="{
  22. width: item.width + 'px',
  23. height: item.height + 'px',
  24. backgroundColor: elColor,
  25. borderRadius: item.width/2 + 'px',
  26. position: 'absolute',
  27. left: (item.left - left) + 'px',
  28. top:(item.top - top)<0?(item.top - top +windowHeight-80)+ 'px':(item.top - top) + 'px'
  29. }"></view>
  30. <view v-for="(item, index) in filletNodes" :key="$u.guid()" :class="animation ? 'skeleton-fade' : ''" :style="{
  31. width: item.width + 'px',
  32. height: item.height + 'px',
  33. backgroundColor: elColor,
  34. borderRadius: borderRadius + 'rpx',
  35. position: 'absolute',
  36. left: (item.left - left) + 'px',
  37. top:(item.top - top)<0?(item.top - top +windowHeight-80)+ 'px':(item.top - top) + 'px'
  38. }">
  39. </view>
  40. </view>
  41. </template>
  42. <script>
  43. /**
  44. * skeleton 骨架屏
  45. * @description 骨架屏一般用于页面在请求远程数据尚未完成时,页面用灰色块预显示本来的页面结构,给用户更好的体验。
  46. * @tutorial https://www.uviewui.com/components/skeleton.html
  47. * @property {String} el-color 骨架块状元素的背景颜色(默认#e5e5e5)
  48. * @property {String} bg-color 骨架组件背景颜色(默认#ffffff)
  49. * @property {Boolean} animation 骨架块是否显示动画效果(默认false)
  50. * @property {String Number} border-radius u-skeleton-fillet类名元素,对应的骨架块的圆角大小,单位rpx(默认10)
  51. * @property {Boolean} loading 是否显示骨架组件,请求完成后,将此值设置为false(默认true)
  52. * @example <u-skeleton :loading="true" :animation="true"></u-skeleton>
  53. */
  54. export default {
  55. name: "u-skeleton",
  56. props: {
  57. // 需要渲染的元素背景颜色,十六进制或者rgb等都可以
  58. elColor: {
  59. type: String,
  60. default: '#e5e5e5'
  61. },
  62. // 整个骨架屏页面的背景颜色
  63. bgColor: {
  64. type: String,
  65. default: '#ffffff'
  66. },
  67. // 是否显示加载动画
  68. animation: {
  69. type: Boolean,
  70. default: false
  71. },
  72. // 圆角值,只对类名为u-skeleton-fillet的元素生效,为数值,不带单位
  73. borderRadius: {
  74. type: [String, Number],
  75. default: "10"
  76. },
  77. // 是否显示骨架,true-显示,false-隐藏
  78. loading: {
  79. type: Boolean,
  80. default: true
  81. }
  82. },
  83. data() {
  84. return {
  85. windowWinth: 750, // 骨架屏宽度
  86. windowHeight: 1500, // 骨架屏高度
  87. filletNodes: [], // 圆角元素
  88. circleNodes: [], // 圆形元素
  89. RectNodes: [], // 矩形元素
  90. top: 0,
  91. left: 0,
  92. }
  93. },
  94. methods: {
  95. // 查询各节点的信息
  96. selecterQueryInfo() {
  97. // 获取整个父组件容器的高度,当做骨架屏的高度
  98. // 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
  99. let query = '';
  100. // #ifdef MP-WEIXIN
  101. query = uni.createSelectorQuery().in(this.$parent);
  102. // #endif
  103. // #ifndef MP-WEIXIN
  104. query = uni.createSelectorQuery()
  105. // #endif
  106. query.selectAll('.u-skeleton').boundingClientRect().exec((res) => {
  107. this.windowHeight = res[0][0].height;
  108. this.windowWinth = res[0][0].width;
  109. res[0][0].bottom = res[0][0].height
  110. this.top = res[0][0].bottom - res[0][0].height;
  111. this.left = res[0][0].left;
  112. });
  113. // 矩形骨架元素
  114. this.getRectEls();
  115. // 圆形骨架元素
  116. this.getCircleEls();
  117. // 圆角骨架元素
  118. this.getFilletEls();
  119. },
  120. // 矩形元素列表
  121. getRectEls() {
  122. let query = '';
  123. // 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
  124. // #ifdef MP-WEIXIN
  125. query = uni.createSelectorQuery().in(this.$parent);
  126. // #endif
  127. // #ifndef MP-WEIXIN
  128. query = uni.createSelectorQuery()
  129. // #endif
  130. query.selectAll('.u-skeleton-rect').boundingClientRect().exec((res) => {
  131. this.RectNodes = res[0];
  132. });
  133. },
  134. // 圆角元素列表
  135. getFilletEls() {
  136. let query = '';
  137. // 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
  138. // #ifdef MP-WEIXIN
  139. query = uni.createSelectorQuery().in(this.$parent);
  140. // #endif
  141. // #ifndef MP-WEIXIN
  142. query = uni.createSelectorQuery()
  143. // #endif
  144. query.selectAll('.u-skeleton-fillet').boundingClientRect().exec((res) => {
  145. this.filletNodes = res[0];
  146. });
  147. },
  148. // 圆形元素列表
  149. getCircleEls() {
  150. let query = '';
  151. // 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
  152. // #ifdef MP-WEIXIN
  153. query = uni.createSelectorQuery().in(this.$parent);
  154. // #endif
  155. // #ifndef MP-WEIXIN
  156. query = uni.createSelectorQuery()
  157. // #endif
  158. query.selectAll('.u-skeleton-circle').boundingClientRect().exec((res) => {
  159. this.circleNodes = res[0];
  160. });
  161. }
  162. },
  163. // 组件被挂载
  164. mounted() {
  165. // 获取系统信息
  166. let systemInfo = uni.getSystemInfoSync();
  167. this.windowHeight = systemInfo.windowHeight;
  168. this.windowWinth = systemInfo.windowWidth;
  169. this.selecterQueryInfo();
  170. }
  171. }
  172. </script>
  173. <style lang="scss" scoped>
  174. .skeleton-fade {
  175. width: 100%;
  176. height: 100%;
  177. background: rgb(194, 207, 214);
  178. animation-duration: 1.5s;
  179. animation-name: blink;
  180. animation-timing-function: ease-in-out;
  181. animation-iteration-count: infinite;
  182. }
  183. @keyframes blink {
  184. 0% {
  185. opacity: 1;
  186. }
  187. 50% {
  188. opacity: 0.4;
  189. }
  190. 100% {
  191. opacity: 1;
  192. }
  193. }
  194. </style>