u-skeleton.vue 7.2 KB

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