综合办公系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uv-skeleton.vue 7.5KB

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