¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="u-skeleton"> |
| | | <view |
| | | class="u-skeleton__wrapper" |
| | | ref="u-skeleton__wrapper" |
| | | v-if="loading" |
| | | style="display: flex; flex-direction: row;" |
| | | > |
| | | <view |
| | | class="u-skeleton__wrapper__avatar" |
| | | v-if="avatar" |
| | | :class="[`u-skeleton__wrapper__avatar--${avatarShape}`, animate && 'animate']" |
| | | :style="{ |
| | | height: $u.addUnit(avatarSize), |
| | | width: $u.addUnit(avatarSize) |
| | | }" |
| | | ></view> |
| | | <view |
| | | class="u-skeleton__wrapper__content" |
| | | ref="u-skeleton__wrapper__content" |
| | | style="flex: 1;" |
| | | > |
| | | <view |
| | | class="u-skeleton__wrapper__content__title" |
| | | v-if="title" |
| | | :style="{ |
| | | width: uTitleWidth, |
| | | height: $u.addUnit(titleHeight), |
| | | }" |
| | | :class="[animate && 'animate']" |
| | | ></view> |
| | | <view |
| | | class="u-skeleton__wrapper__content__rows" |
| | | :class="[animate && 'animate']" |
| | | v-for="(item, index) in rowsArray" |
| | | :key="index" |
| | | :style="{ |
| | | width: item.width, |
| | | height: item.height, |
| | | marginTop: item.marginTop |
| | | }" |
| | | > |
| | | |
| | | </view> |
| | | </view> |
| | | </view> |
| | | <slot v-else /> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | // #ifdef APP-NVUE |
| | | // ç±äºweex为é¿éçKPIä¸ç»©èæ ¸ç产ç©ï¼æä»¥ä¸æ¯æç¾åæ¯åä½ï¼è¿ééè¦éè¿domæ¥è¯¢ç»ä»¶ç宽度 |
| | | const dom = uni.requireNativePlugin('dom') |
| | | const animation = uni.requireNativePlugin('animation') |
| | | // #endif |
| | | /** |
| | | * Skeleton 骨æ¶å± |
| | | * @description 骨æ¶å±ä¸è¬ç¨äºé¡µé¢å¨è¯·æ±è¿ç¨æ°æ®å°æªå®ææ¶ï¼é¡µé¢ç¨ç°è²å颿¾ç¤ºæ¬æ¥ç页é¢ç»æï¼ç»ç¨æ·æ´å¥½çä½éªã |
| | | * @tutorial https://www.uviewui.com/components/skeleton.html |
| | | * @property {Boolean} loading æ¯å¦æ¾ç¤ºéª¨æ¶å ä½å¾ï¼è®¾ç½®ä¸ºfalseå°ä¼å±ç¤ºåç»ä»¶å
容 (é»è®¤ true ) |
| | | * @property {Boolean} animate æ¯å¦å¼å¯å¨ç»ææ (é»è®¤ true ) |
| | | * @property {String | Number} rows 段è½å ä½å¾è¡æ° (é»è®¤ 0 ) |
| | | * @property {String | Number | Array} rowsWidth 段è½å ä½å¾ç宽度ï¼å¯ä»¥ä¸ºç¾åæ¯ï¼æ°å¼ï¼å¸¦åä½å符串çï¼å¯éè¿æ°ç»ä¼ å
¥æå®æ¯ä¸ªæ®µè½è¡ç宽度 (é»è®¤ '100%' ) |
| | | * @property {String | Number | Array} rowsHeight 段è½çé«åº¦ (é»è®¤ 18 ) |
| | | * @property {Boolean} title æ¯å¦å±ç¤ºæ é¢å ä½å¾ (é»è®¤ true ) |
| | | * @property {String | Number} titleWidth æ é¢ç宽度 (é»è®¤ '50%' ) |
| | | * @property {String | Number} titleHeight æ é¢çé«åº¦ (é»è®¤ 18 ) |
| | | * @property {Boolean} avatar æ¯å¦å±ç¤ºå¤´åå ä½å¾ (é»è®¤ false ) |
| | | * @property {String | Number} avatarSize 头åå ä½å¾å¤§å° (é»è®¤ 32 ) |
| | | * @property {String} avatarShape 头åå ä½å¾çå½¢ç¶ï¼circle-åå½¢ï¼square-æ¹å½¢ (é»è®¤ 'circle' ) |
| | | * @example <u-search placeholder="æ¥ç
§é¦ççç´«ç" v-model="keyword"></u-search> |
| | | */ |
| | | export default { |
| | | name: 'u-skeleton', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
| | | data() { |
| | | return { |
| | | width: 0, |
| | | } |
| | | }, |
| | | watch: { |
| | | loading() { |
| | | this.getComponentWidth() |
| | | } |
| | | }, |
| | | computed: { |
| | | rowsArray() { |
| | | if (/%$/.test(this.rowsHeight)) { |
| | | uni.$u.error('rowsHeight忰䏿¯æç¾åæ¯åä½') |
| | | } |
| | | const rows = [] |
| | | for (let i = 0; i < this.rows; i++) { |
| | | let item = {}, |
| | | // éè¦é¢é²è¶
åºæ°ç»è¾¹ççæ
åµ |
| | | rowWidth = uni.$u.test.array(this.rowsWidth) ? (this.rowsWidth[i] || (i === this.row - 1 ? '70%' : '100%')) : i === |
| | | this.rows - 1 ? '70%' : this.rowsWidth, |
| | | rowHeight = uni.$u.test.array(this.rowsHeight) ? (this.rowsHeight[i] || '18px') : this.rowsHeight |
| | | // 妿ætitleå ä½å¾ï¼ç¬¬ä¸ä¸ªæ®µè½å ä½å¾çå¤è¾¹è·éè¦å¤§ä¸äºï¼å¦ææ²¡ætitleå ä½å¾ï¼ç¬¬ä¸ä¸ªæ®µè½å ä½å¾åæ éå¤è¾¹è· |
| | | // 乿以éè¦è¿ä¹åï¼æ¯å 为weexçæ è½ï¼ä»¥æåæ§è½ä¸ºåå£ä¸æ¯æcssçä¸äºä¼ªç±» |
| | | item.marginTop = !this.title && i === 0 ? 0 : this.title && i === 0 ? '20px' : '12px' |
| | | // å¦æè®¾ç½®ç为ç¾åæ¯ç宽度ï¼è½¬æ¢ä¸ºpxå¼ï¼å 为nvue䏿¯æç¾åæ¯åä½ |
| | | if (/%$/.test(rowWidth)) { |
| | | // éè¿parseIntæååºç¾åæ¯åä½ä¸çæ°å¼é¨åï¼é¤ä»¥100å¾å°ç¾åæ¯çå°æ°å¼ |
| | | item.width = uni.$u.addUnit(this.width * parseInt(rowWidth) / 100) |
| | | } else { |
| | | item.width = uni.$u.addUnit(rowWidth) |
| | | } |
| | | item.height = uni.$u.addUnit(rowHeight) |
| | | rows.push(item) |
| | | } |
| | | // console.log(rows); |
| | | return rows |
| | | }, |
| | | uTitleWidth() { |
| | | let tWidth = 0 |
| | | if (/%$/.test(this.titleWidth)) { |
| | | // éè¿parseIntæååºç¾åæ¯åä½ä¸çæ°å¼é¨åï¼é¤ä»¥100å¾å°ç¾åæ¯çå°æ°å¼ |
| | | tWidth = uni.$u.addUnit(this.width * parseInt(this.titleWidth) / 100) |
| | | } else { |
| | | tWidth = uni.$u.addUnit(this.titleWidth) |
| | | } |
| | | return uni.$u.addUnit(tWidth) |
| | | }, |
| | | |
| | | }, |
| | | mounted() { |
| | | this.init() |
| | | }, |
| | | methods: { |
| | | init() { |
| | | this.getComponentWidth() |
| | | // #ifdef APP-NVUE |
| | | this.loading && this.animate && this.setNvueAnimation() |
| | | // #endif |
| | | }, |
| | | async setNvueAnimation() { |
| | | // #ifdef APP-NVUE |
| | | // 为äºè®©opacity:1çç¶æä¿æä¸å®æ¶é´ï¼è¿éåä¸ä¸ªå»¶æ¶ |
| | | await uni.$u.sleep(500) |
| | | const skeleton = this.$refs['u-skeleton__wrapper']; |
| | | skeleton && this.loading && this.animate && animation.transition(skeleton, { |
| | | styles: { |
| | | opacity: 0.5 |
| | | }, |
| | | duration: 600, |
| | | }, () => { |
| | | // è¿éæ é夿æ¯å¦loadingåå¼å¯å¨ç»ç¶æï¼å 为æç»çç¶æå¿
须达å°opacity: 1ï¼å¦åå¯è½ |
| | | // ä¼åçå¨opacity: 0.5çç¶æä¸ |
| | | animation.transition(skeleton, { |
| | | styles: { |
| | | opacity: 1 |
| | | }, |
| | | duration: 600, |
| | | }, () => { |
| | | // åªæå¨loading䏿¶ï¼ææ§è¡å¨ç» |
| | | this.loading && this.animate && this.setNvueAnimation() |
| | | }) |
| | | }) |
| | | // #endif |
| | | }, |
| | | // è·åç»ä»¶ç宽度 |
| | | async getComponentWidth() { |
| | | // å»¶æ¶ä¸å®æ¶é´ï¼ä»¥è·ådom尺寸 |
| | | await uni.$u.sleep(20) |
| | | // #ifndef APP-NVUE |
| | | this.$uGetRect('.u-skeleton__wrapper__content').then(size => { |
| | | this.width = size.width |
| | | }) |
| | | // #endif |
| | | |
| | | // #ifdef APP-NVUE |
| | | const ref = this.$refs['u-skeleton__wrapper__content'] |
| | | ref && dom.getComponentRect(ref, (res) => { |
| | | this.width = res.size.width |
| | | }) |
| | | // #endif |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | @mixin background { |
| | | /* #ifdef APP-NVUE */ |
| | | background-color: #F1F2F4; |
| | | /* #endif */ |
| | | /* #ifndef APP-NVUE */ |
| | | background: linear-gradient(90deg, #F1F2F4 25%, #e6e6e6 37%, #F1F2F4 50%); |
| | | background-size: 400% 100%; |
| | | /* #endif */ |
| | | } |
| | | |
| | | .u-skeleton { |
| | | flex: 1; |
| | | |
| | | &__wrapper { |
| | | @include flex(row); |
| | | |
| | | &__avatar { |
| | | @include background; |
| | | margin-right: 15px; |
| | | |
| | | &--circle { |
| | | border-radius: 100px; |
| | | } |
| | | |
| | | &--square { |
| | | border-radius: 4px; |
| | | } |
| | | } |
| | | |
| | | &__content { |
| | | flex: 1; |
| | | |
| | | &__rows, |
| | | &__title { |
| | | @include background; |
| | | border-radius: 3px; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | /* #ifndef APP-NVUE */ |
| | | .animate { |
| | | animation: skeleton 1.8s ease infinite |
| | | } |
| | | |
| | | @keyframes skeleton { |
| | | 0% { |
| | | background-position: 100% 50% |
| | | } |
| | | |
| | | 100% { |
| | | background-position: 0 50% |
| | | } |
| | | } |
| | | |
| | | /* #endif */ |
| | | </style> |