u-skeleton.vue 7.1 KB

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