index.vue 6.7 KB

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