u-skeleton.vue 5.8 KB

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