<template>
<view class>
<view
class="u-content"
:class="[elId]"
:style="{ height: isLongContent && !showMore ? showHeight + 'rpx' : 'auto' }"
>
<slot></slot>
</view>
<view
@tap="toggleReadMore"
v-if="isLongContent"
class="u-showmore-wrap"
:class="{ 'u-show-more': showMore }"
:style="[innerShadowStyle]"
>
<text
class="u-readmore-btn"
:style="{
fontSize: fontSize + 'rpx',
color: color
}"
>{{ showMore ? openText : closeText }}</text>
<u-icon
:color="color"
:size="fontSize"
class="u-icon"
:name="showMore ? 'arrow-up' : 'arrow-down'"
></u-icon>
</view>
</view>
</template>
<script>
export default {
name: 'u-read-more',
props: {
showHeight: {
type: [Number, String],
default: 200
},
toggle: {
type: Boolean,
default: false
},
closeText: {
type: String,
default: '展开阅读全文'
},
openText: {
type: String,
default: '收起'
},
color: {
type: String,
default: '#2979ff'
},
fontSize: {
type: [String, Number],
default: 14
},
shadowStyle: {
type: Object,
default() {
return {
backgroundImage:
'linear-gradient(-180deg, rgba(255, 255, 255, 0) 0%, #fff 80%)',
paddingTop: '150rpx',
marginTop: '-150rpx'
}
}
}
},
watch: {
paramsChange(val) {
this.init()
}
},
computed: {
paramsChange() {
return `${this.toggle}-${this.showHeight}`
},
innerShadowStyle() {
if (this.showMore) return {}
else return this.shadowStyle
}
},
data() {
return {
isLongContent: false,
showMore: false,
elId: this.$u.guid()
}
},
mounted() {
this.init()
},
methods: {
init() {
this.$uGetRect('.' + this.elId).then(res => {
if (res.height > uni.upx2px(this.showHeight)) {
this.isLongContent = true
this.showMore = false
}
})
},
toggleReadMore() {
this.showMore = !this.showMore
if (this.toggle == false) this.isLongContent = false
}
}
}
</script>
<style lang="scss" scoped>
@import '../../libs/css/style.components.scss';
.u-content {
font-size: 15rpx;
color: $u-content-color;
line-height: 1.8;
text-align: left;
text-indent: 2em;
overflow: hidden;
}
.u-showmore-wrap {
position: relative;
width: 100%;
padding-bottom: 13rpx;
display: flex;
align-items: center;
justify-content: center;
}
.u-show-more {
padding-top: 0;
background: none;
margin-top: 10rpx;
}
.u-readmore-btn {
display: flex;
align-items: center;
justify-content: center;
line-height: 1;
}
.u-icon {
margin-left: 7rpx;
}
</style>