¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="u-read-more"> |
| | | <view |
| | | class="u-read-more__content" |
| | | :style="{ |
| | | height: isLongContent && status === 'close' ? $u.addUnit(showHeight) : $u.addUnit(contentHeight), |
| | | textIndent: textIndent |
| | | }" |
| | | > |
| | | <view |
| | | class="u-read-more__content__inner" |
| | | ref="u-read-more__content__inner" |
| | | :class="[elId]" |
| | | > |
| | | <slot></slot> |
| | | </view> |
| | | </view> |
| | | <view |
| | | class="u-read-more__toggle" |
| | | :style="[innerShadowStyle]" |
| | | v-if="isLongContent" |
| | | > |
| | | <slot name="toggle"> |
| | | <view |
| | | class="u-read-more__toggle__text" |
| | | @tap="toggleReadMore" |
| | | > |
| | | <u--text |
| | | :text="status === 'close' ? closeText : openText" |
| | | :color="color" |
| | | :size="fontSize" |
| | | :lineHeight="fontSize" |
| | | margin="0 5px 0 0" |
| | | ></u--text> |
| | | <view class="u-read-more__toggle__icon"> |
| | | <u-icon |
| | | :color="color" |
| | | :size="fontSize + 2" |
| | | :name="status === 'close' ? 'arrow-down' : 'arrow-up'" |
| | | ></u-icon> |
| | | </view> |
| | | </view> |
| | | </slot> |
| | | </view> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | // #ifdef APP-NVUE |
| | | const dom = uni.requireNativePlugin('dom') |
| | | // #endif |
| | | import props from './props.js'; |
| | | /** |
| | | * readMore é
读æ´å¤ |
| | | * @description 该ç»ä»¶ä¸è¬ç¨äºå
容è¾é¿ï¼é¢å
æ¶èµ·ä¸é¨åï¼ç¹å»å±å¼å
¨é¨å
容çåºæ¯ã |
| | | * @tutorial https://www.uviewui.com/components/readMore.html |
| | | * @property {String | Number} showHeight å
容è¶
åºæ¤é«åº¦æä¼æ¾ç¤ºå±å¼å
¨ææé®ï¼åä½pxï¼é»è®¤ 400 ï¼ |
| | | * @property {Boolean} toggle å±å¼åæ¯å¦æ¾ç¤ºæ¶èµ·æé®ï¼é»è®¤ false ï¼ |
| | | * @property {String} closeText å
³éæ¶çæç¤ºæåï¼é»è®¤ 'å±å¼é
读å
¨æ' ï¼ |
| | | * @property {String} openText å±å¼æ¶çæç¤ºæåï¼é»è®¤ 'æ¶èµ·' ï¼ |
| | | * @property {String} color æç¤ºæåçé¢è²ï¼é»è®¤ '#2979ff' ï¼ |
| | | * @property {String | Number} fontSize æç¤ºæåç大å°ï¼åä½px ï¼é»è®¤ 14 ï¼ |
| | | * @property {Object} shadowStyle æ¾ç¤ºé´å½±çæ ·å¼ |
| | | * @property {String} textIndent 段è½é¦è¡ç¼©è¿çåç¬¦ä¸ªæ° ï¼é»è®¤ '2em' ï¼ |
| | | * @property {String | Number} name ç¨äºå¨ open å close äºä»¶ä¸å½ä½åè°åæ°è¿å |
| | | * @event {Function} open å
容被å±å¼æ¶è§¦å |
| | | * @event {Function} close å
容被æ¶èµ·æ¶è§¦å |
| | | * @example <u-read-more><rich-text :nodes="content"></rich-text></u-read-more> |
| | | */ |
| | | export default { |
| | | name: 'u-read-more', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
| | | data() { |
| | | return { |
| | | isLongContent: false, // æ¯å¦éè¦éèä¸é¨åå
容 |
| | | status: 'close', // å½åéè䏿¾ç¤ºçç¶æï¼close-æ¶èµ·ç¶æï¼open-å±å¼ç¶æ |
| | | elId: uni.$u.guid(), // çæå¯ä¸class |
| | | contentHeight: 100, // å
容é«åº¦ |
| | | } |
| | | }, |
| | | computed: { |
| | | // å±å¼åæ éé´å½±ï¼æ¶èµ·æ¶æéè¦é´å½±æ ·å¼ |
| | | innerShadowStyle() { |
| | | if (this.status === 'open') return {} |
| | | else return this.shadowStyle |
| | | } |
| | | }, |
| | | mounted() { |
| | | this.init() |
| | | }, |
| | | methods: { |
| | | async init() { |
| | | this.getContentHeight().then(height => { |
| | | this.contentHeight = height |
| | | // 夿é«åº¦ï¼å¦æçå®å
容é«åº¦å¤§äºå ä½é«åº¦ï¼åæ¾ç¤ºæ¶èµ·ä¸å±å¼çæ§å¶æé® |
| | | if (height > uni.$u.getPx(this.showHeight)) { |
| | | this.isLongContent = true |
| | | this.status = 'close' |
| | | } |
| | | }) |
| | | }, |
| | | // è·åå
容çé«åº¦ |
| | | async getContentHeight() { |
| | | // å»¶æ¶ä¸å®æ¶é´åè·åèç¹ |
| | | await uni.$u.sleep(30) |
| | | return new Promise(resolve => { |
| | | // #ifndef APP-NVUE |
| | | this.$uGetRect('.' + this.elId).then(res => { |
| | | resolve(res.height) |
| | | }) |
| | | // #endif |
| | | |
| | | // #ifdef APP-NVUE |
| | | const ref = this.$refs['u-read-more__content__inner'] |
| | | dom.getComponentRect(ref, (res) => { |
| | | resolve(res.size.height) |
| | | }) |
| | | // #endif |
| | | }) |
| | | }, |
| | | // å±å¼æè
æ¶èµ· |
| | | toggleReadMore() { |
| | | this.status = this.status === 'close' ? 'open' : 'close' |
| | | // 妿toggle为falseï¼éè"æ¶èµ·"é¨åçå
容 |
| | | if (this.toggle == false) this.isLongContent = false |
| | | // ååºæå¼æè
æ¶é½çäºä»¶ |
| | | this.$emit(this.status, this.name) |
| | | } |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | |
| | | .u-read-more { |
| | | |
| | | &__content { |
| | | overflow: hidden; |
| | | color: $u-content-color; |
| | | font-size: 15px; |
| | | text-align: left; |
| | | } |
| | | |
| | | &__toggle { |
| | | @include flex; |
| | | justify-content: center; |
| | | |
| | | &__text { |
| | | @include flex; |
| | | align-items: center; |
| | | justify-content: center; |
| | | margin-top: 5px; |
| | | } |
| | | } |
| | | } |
| | | </style> |