多租户商城-商户小程序端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.9 KiB

2 years ago
2 years ago
  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: 9998,
  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) + '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) + '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) + 'px'
  38. }"></view>
  39. </view>
  40. </template>
  41. <script>
  42. /**
  43. * skeleton 骨架屏
  44. * @description 骨架屏一般用于页面在请求远程数据尚未完成时页面用灰色块预显示本来的页面结构给用户更好的体验
  45. * @tutorial https://www.uviewui.com/components/skeleton.html
  46. * @property {String} el-color 骨架块状元素的背景颜色默认#e5e5e5
  47. * @property {String} bg-color 骨架组件背景颜色默认#ffffff
  48. * @property {Boolean} animation 骨架块是否显示动画效果默认false
  49. * @property {String Number} border-radius u-skeleton-fillet类名元素对应的骨架块的圆角大小单位rpx默认10
  50. * @property {Boolean} loading 是否显示骨架组件请求完成后将此值设置为false默认true
  51. * @example <u-skeleton :loading="true" :animation="true"></u-skeleton>
  52. */
  53. export default {
  54. name: "u-skeleton",
  55. props: {
  56. // 需要渲染的元素背景颜色,十六进制或者rgb等都可以
  57. elColor: {
  58. type: String,
  59. default: '#e5e5e5'
  60. },
  61. // 整个骨架屏页面的背景颜色
  62. bgColor: {
  63. type: String,
  64. default: '#ffffff'
  65. },
  66. // 是否显示加载动画
  67. animation: {
  68. type: Boolean,
  69. default: false
  70. },
  71. // 圆角值,只对类名为u-skeleton-fillet的元素生效,为数值,不带单位
  72. borderRadius: {
  73. type: [String, Number],
  74. default: "10"
  75. },
  76. // 是否显示骨架,true-显示,false-隐藏
  77. loading: {
  78. type: Boolean,
  79. default: true
  80. }
  81. },
  82. data() {
  83. return {
  84. windowWinth: 750, // 骨架屏宽度
  85. windowHeight: 1500, // 骨架屏高度
  86. filletNodes: [], // 圆角元素
  87. circleNodes: [], // 圆形元素
  88. RectNodes: [], // 矩形元素
  89. top: 0,
  90. left: 0,
  91. }
  92. },
  93. methods: {
  94. // 查询各节点的信息
  95. selecterQueryInfo() {
  96. // 获取整个父组件容器的高度,当做骨架屏的高度
  97. // 在微信小程序中,如果把骨架屏放入组件中使用的话,需要调in(this)上下文为父组件才有效
  98. let query = '';
  99. // #ifdef MP-WEIXIN
  100. query = uni.createSelectorQuery().in(this.$parent);
  101. // #endif
  102. // #ifndef MP-WEIXIN
  103. query = uni.createSelectorQuery()
  104. // #endif
  105. query.selectAll('.u-skeleton').boundingClientRect().exec((res) => {
  106. if(res[0][0]){
  107. this.windowHeight = res[0][0].height;
  108. this.windowWinth = res[0][0].width;
  109. this.top = res[0][0].bottom - res[0][0].height;
  110. this.left = res[0][0].left;
  111. }
  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. @import "../../libs/css/style.components.scss";
  175. .skeleton-fade {
  176. width: 100%;
  177. height: 100%;
  178. background: rgb(194, 207, 214);
  179. animation-duration: 1.5s;
  180. animation-name: blink;
  181. animation-timing-function: ease-in-out;
  182. animation-iteration-count: infinite;
  183. }
  184. @keyframes blink {
  185. 0% {
  186. opacity: 1;
  187. }
  188. 50% {
  189. opacity: 0.4;
  190. }
  191. 100% {
  192. opacity: 1;
  193. }
  194. }
  195. </style>