<template>
<view class="common-article-wrap" :style="{'maxHeight':`${!isVisible ? 38*(maxShowLine + 1)+'rpx' : 'auto'}`,'overflow':`${!isVisible ? 'hidden' : 'visible'}`,'marginBottom':`${!isVisible ? '0rpx' : '38rpx'}`}">
<view class="common-article-body">
<text v-text="text"></text>
</view>
<view class="common-show-more-wrap">
<view class="placeholder-text-height"></view>
<view class="placeholder-text-line-height" :style="{'height':`${!isVisible ? 38*(maxShowLine)+'rpx' : '100%'}`}"></view>
<view class="common-show-more" :style="{'backgroundColor':backgroundColor}" @tap="toggleVisible">
<text v-text="visibleText"></text>
</view>
</view>
</view>
</template>
<script>
export default{
name:"CommonShowMore",
data(){
return {
isVisible:false, // 是否显示隐藏
maxShowLine:2, // 最大显示多少行
}
},
computed:{
visibleText(){
return !this.isVisible ? '查看原文' : '收起'
}
},
methods:{
toggleVisible(){
this.isVisible = !this.isVisible;
}
},
props:{
// 显示的纯文本内容,不兼容z-index 比较高的内容
text:{
type:String,
default:''
},
backgroundColor:{
type:String,
default:"#FFFFFF"
}
}
}
</script>
<style lang="scss">
$lineHeight:38rpx;
.common-article-wrap{
overflow: hidden;
font-size: 30rpx;
line-height: $lineHeight;
font-weight: 400;
color:#3C3C3C;
position: relative;
.common-article-body{
&>text{
word-break: break-all;
white-space: pre-wrap;
hyphens: auto;
}
}
.common-show-more-wrap{
position:absolute;
top: 0;
left: 0%;
width: 100%;
height: 100%;
.placeholder-text-height{
float:right;
height: 100%;
width:1rpx;
//width:1%;
}
.placeholder-text-line-height{
float:right;
width:calc(100% - 1rpx);
//width:99%;
}
.common-show-more{
float:left;
width:calc(100% - 1rpx);
//width:99%;
height:$lineHeight;
line-height:$lineHeight;
z-index:2;
color: #5B7DFE;
}
&:after{
clear:both;
content:'';
}
}
}
</style>
【使用 uni-app 做的,使用 css 的解决方案】【参考链接 https://www.cnblogs.com/wetest/p/7365676.html】

