Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 

254 рядки
7.4 KiB

  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://uview-plus.jiangruyi.com/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. @mixin background {
  186. /* #ifdef APP-NVUE */
  187. background-color: var(--up-skeleton-bg-color, var(--up-fill-color, #2f3135));
  188. /* #endif */
  189. /* #ifndef APP-NVUE */
  190. background: linear-gradient(
  191. 90deg,
  192. var(--up-skeleton-bg-color, var(--up-fill-color, #2f3135)) 25%,
  193. var(--up-skeleton-shimmer-color, rgba(255, 255, 255, 0.12)) 37%,
  194. var(--up-skeleton-bg-color, var(--up-fill-color, #2f3135)) 50%
  195. );
  196. background-size: 400% 100%;
  197. /* #endif */
  198. }
  199. .u-skeleton {
  200. flex: 1;
  201. &__wrapper {
  202. @include flex(row);
  203. &__avatar {
  204. @include background;
  205. margin-right: 15px;
  206. &--circle {
  207. border-radius: 100px;
  208. }
  209. &--square {
  210. border-radius: 4px;
  211. }
  212. }
  213. &__content {
  214. flex: 1;
  215. &__rows,
  216. &__title {
  217. @include background;
  218. border-radius: 3px;
  219. }
  220. }
  221. }
  222. }
  223. /* #ifndef APP-NVUE */
  224. .animate {
  225. animation: skeleton 1.8s ease infinite
  226. }
  227. @keyframes skeleton {
  228. 0% {
  229. background-position: 100% 50%
  230. }
  231. 100% {
  232. background-position: 0 50%
  233. }
  234. }
  235. /* #endif */
  236. </style>