u-skeleton.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <template>
  2. <view class="u-skeleton">
  3. <view
  4. class="u-skeleton__wrapper"
  5. ref="u-skeleton__wrapper"
  6. v-if="loading"
  7. style="display: flex; flex-direction: row;"
  8. >
  9. <view
  10. class="u-skeleton__wrapper__avatar"
  11. v-if="avatar"
  12. :class="[`u-skeleton__wrapper__avatar--${avatarShape}`, animate && 'animate']"
  13. :style="{
  14. height: $u.addUnit(avatarSize),
  15. width: $u.addUnit(avatarSize)
  16. }"
  17. ></view>
  18. <view
  19. class="u-skeleton__wrapper__content"
  20. ref="u-skeleton__wrapper__content"
  21. style="flex: 1;"
  22. >
  23. <view
  24. class="u-skeleton__wrapper__content__title"
  25. v-if="title"
  26. :style="{
  27. width: uTitleWidth,
  28. height: $u.addUnit(titleHeight),
  29. }"
  30. :class="[animate && 'animate']"
  31. ></view>
  32. <view
  33. class="u-skeleton__wrapper__content__rows"
  34. :class="[animate && 'animate']"
  35. v-for="(item, index) in rowsArray"
  36. :key="index"
  37. :style="{
  38. width: item.width,
  39. height: item.height,
  40. marginTop: item.marginTop
  41. }"
  42. >
  43. </view>
  44. </view>
  45. </view>
  46. <slot v-else />
  47. </view>
  48. </template>
  49. <script>
  50. import props from './props.js';
  51. // #ifdef APP-NVUE
  52. // 由于weex为阿里的KPI业绩考核的产物,所以不支持百分比单位,这里需要通过dom查询组件的宽度
  53. const dom = uni.requireNativePlugin('dom')
  54. const animation = uni.requireNativePlugin('animation')
  55. // #endif
  56. /**
  57. * Skeleton 骨架屏
  58. * @description 骨架屏一般用于页面在请求远程数据尚未完成时,页面用灰色块预显示本来的页面结构,给用户更好的体验。
  59. * @tutorial https://www.uviewui.com/components/skeleton.html
  60. * @property {Boolean} loading 是否显示骨架占位图,设置为false将会展示子组件内容 (默认 true )
  61. * @property {Boolean} animate 是否开启动画效果 (默认 true )
  62. * @property {String | Number} rows 段落占位图行数 (默认 0 )
  63. * @property {String | Number | Array} rowsWidth 段落占位图的宽度,可以为百分比,数值,带单位字符串等,可通过数组传入指定每个段落行的宽度 (默认 '100%' )
  64. * @property {String | Number | Array} rowsHeight 段落的高度 (默认 18 )
  65. * @property {Boolean} title 是否展示标题占位图 (默认 true )
  66. * @property {String | Number} titleWidth 标题的宽度 (默认 '50%' )
  67. * @property {String | Number} titleHeight 标题的高度 (默认 18 )
  68. * @property {Boolean} avatar 是否展示头像占位图 (默认 false )
  69. * @property {String | Number} avatarSize 头像占位图大小 (默认 32 )
  70. * @property {String} avatarShape 头像占位图的形状,circle-圆形,square-方形 (默认 'circle' )
  71. * @example <u-search placeholder="日照香炉生紫烟" v-model="keyword"></u-search>
  72. */
  73. export default {
  74. name: 'u-skeleton',
  75. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  76. data() {
  77. return {
  78. width: 0,
  79. }
  80. },
  81. watch: {
  82. loading() {
  83. this.getComponentWidth()
  84. }
  85. },
  86. computed: {
  87. rowsArray() {
  88. if (/%$/.test(this.rowsHeight)) {
  89. uni.$u.error('rowsHeight参数不支持百分比单位')
  90. }
  91. const rows = []
  92. for (let i = 0; i < this.rows; i++) {
  93. let item = {},
  94. // 需要预防超出数组边界的情况
  95. rowWidth = uni.$u.test.array(this.rowsWidth) ? (this.rowsWidth[i] || (i === this.row - 1 ? '70%' : '100%')) : i ===
  96. this.rows - 1 ? '70%' : this.rowsWidth,
  97. rowHeight = uni.$u.test.array(this.rowsHeight) ? (this.rowsHeight[i] || '18px') : this.rowsHeight
  98. // 如果有title占位图,第一个段落占位图的外边距需要大一些,如果没有title占位图,第一个段落占位图则无需外边距
  99. // 之所以需要这么做,是因为weex的无能,以提升性能为借口不支持css的一些伪类
  100. item.marginTop = !this.title && i === 0 ? 0 : this.title && i === 0 ? '20px' : '12px'
  101. // 如果设置的为百分比的宽度,转换为px值,因为nvue不支持百分比单位
  102. if (/%$/.test(rowWidth)) {
  103. // 通过parseInt提取出百分比单位中的数值部分,除以100得到百分比的小数值
  104. item.width = uni.$u.addUnit(this.width * parseInt(rowWidth) / 100)
  105. } else {
  106. item.width = uni.$u.addUnit(rowWidth)
  107. }
  108. item.height = uni.$u.addUnit(rowHeight)
  109. rows.push(item)
  110. }
  111. return rows
  112. },
  113. uTitleWidth() {
  114. let tWidth = 0
  115. if (/%$/.test(this.titleWidth)) {
  116. // 通过parseInt提取出百分比单位中的数值部分,除以100得到百分比的小数值
  117. tWidth = uni.$u.addUnit(this.width * parseInt(this.titleWidth) / 100)
  118. } else {
  119. tWidth = uni.$u.addUnit(this.titleWidth)
  120. }
  121. return uni.$u.addUnit(tWidth)
  122. },
  123. },
  124. mounted() {
  125. this.init()
  126. },
  127. methods: {
  128. init() {
  129. this.getComponentWidth()
  130. // #ifdef APP-NVUE
  131. this.loading && this.animate && this.setNvueAnimation()
  132. // #endif
  133. },
  134. async setNvueAnimation() {
  135. // #ifdef APP-NVUE
  136. // 为了让opacity:1的状态保持一定时间,这里做一个延时
  137. await uni.$u.sleep(500)
  138. const skeleton = this.$refs['u-skeleton__wrapper'];
  139. skeleton && this.loading && this.animate && animation.transition(skeleton, {
  140. styles: {
  141. opacity: 0.5
  142. },
  143. duration: 600,
  144. }, () => {
  145. // 这里无需判断是否loading和开启动画状态,因为最终的状态必须达到opacity: 1,否则可能
  146. // 会停留在opacity: 0.5的状态中
  147. animation.transition(skeleton, {
  148. styles: {
  149. opacity: 1
  150. },
  151. duration: 600,
  152. }, () => {
  153. // 只有在loading中时,才执行动画
  154. this.loading && this.animate && this.setNvueAnimation()
  155. })
  156. })
  157. // #endif
  158. },
  159. // 获取组件的宽度
  160. async getComponentWidth() {
  161. // 延时一定时间,以获取dom尺寸
  162. await uni.$u.sleep(20)
  163. // #ifndef APP-NVUE
  164. this.$uGetRect('.u-skeleton__wrapper__content').then(size => {
  165. this.width = size.width
  166. })
  167. // #endif
  168. // #ifdef APP-NVUE
  169. const ref = this.$refs['u-skeleton__wrapper__content']
  170. ref && dom.getComponentRect(ref, (res) => {
  171. this.width = res.size.width
  172. })
  173. // #endif
  174. }
  175. }
  176. }
  177. </script>
  178. <style lang="scss" scoped>
  179. @import "../../libs/css/components.scss";
  180. @mixin background {
  181. /* #ifdef APP-NVUE */
  182. background-color: #F1F2F4;
  183. /* #endif */
  184. /* #ifndef APP-NVUE */
  185. background: linear-gradient(90deg, #F1F2F4 25%, #e6e6e6 37%, #F1F2F4 50%);
  186. background-size: 400% 100%;
  187. /* #endif */
  188. }
  189. .u-skeleton {
  190. flex: 1;
  191. &__wrapper {
  192. @include flex(row);
  193. &__avatar {
  194. @include background;
  195. margin-right: 15px;
  196. &--circle {
  197. border-radius: 100px;
  198. }
  199. &--square {
  200. border-radius: 4px;
  201. }
  202. }
  203. &__content {
  204. flex: 1;
  205. &__rows,
  206. &__title {
  207. @include background;
  208. border-radius: 3px;
  209. }
  210. }
  211. }
  212. }
  213. /* #ifndef APP-NVUE */
  214. .animate {
  215. animation: skeleton 1.8s ease infinite
  216. }
  217. @keyframes skeleton {
  218. 0% {
  219. background-position: 100% 50%
  220. }
  221. 100% {
  222. background-position: 0 50%
  223. }
  224. }
  225. /* #endif */
  226. </style>