div部分
<div class="immemoInfo" >
<p ref="memocontent" :class="{ statusmemoText: showMore[index].value }">{{activity.memorandumContent}}</p>
<div class="moremebtn" v-if="showMore[index].value != false" @click="moreClick(index)">...查看更多</div>
</div>
css部分
.immemoInfo{
padding: 12px 16px;
background: #2b21730d;
color: #000000;
font-size: 14px;
border-radius: 4px;
position: relative;
overflow: hidden;
}
.immemoInfo p{
margin: 0px;
line-height: 22px;
}
.statusmemoText {
overflow : hidden ; text-overflow : ellipsis ; display : -webkit-box ; -webkit-line-clamp : 3 ; -webkit-box-orient : vertical ;
}
.moremebtn{
position: absolute;
right: 16px;
bottom: 15px;
cursor: pointer;
background-color: #F4F4F8;
color: #2D60ED;
}
.moremebtn:before {
position: absolute;
right: 100%;
content: "";
width:0.16rem;
height:0.22rem;
background-image: linear-gradient(270deg,#fff,hsla(0,0%,100%,0));
}
JS部分
return:{
showMore: [
{ value: false },
{ value: false },
{ value: false },
{ value: false },
{ value: false }
]
}
methods:{
melistHeight(){
let that=this;
this.showMore=[
{ value: false },
{ value: false },
{ value: false },
{ value: false },
{ value: false }]
that.$nextTick(() => {
let showMore = []
//遍历this.activities中的每一个元素。对于每一个元素,向showMore数组添加一个新的对象,这个对象的value属性初始值为false。这样,showMore数组的长度与this.dynamicArray相同,每个对应的项都初始化为false。
for (let i = 0; i < that.activities.length; i++) {
showMore.push({ value: false })
}
//获取名为listItems的Vue引用(Vue中用$refs来获取子组件或DOM元素)。
const listItems = that.$refs.memocontent
if (listItems) {// 如果listItems存在,执行以下操作:
listItems.forEach((li, index) => {//对listItems中的每个元素进行遍历(使用forEach方法)。
const contentHeight = li.offsetHeight // 对于每个元素,通过li.querySelector('p').offsetHeight获取该元素下<p>标签的高度(以像素为单位)因为遍历的p标签所以直接去li.offsetHeight 。
if (contentHeight > 60) {//如果段落高度大于60那么就显示查看更多
that.showMore[index].value = true
} else {
that.showMore[index].value = false
}
})
}
})
},
moreClick(index){
this.showMore[index].value=false
},
}