u-skeleton.vue 7.2 KB

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