纯文本内容展开收起(可套用)
html
<div class="brief_introduction">
<input type="checkbox" id="exp" />
<div class="text">
<label class="btn" for="exp" ref="btn"></label>
<p ref="text">
简介:我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本我是一大段文本
</p>
</div>
</div>
css
.brief_introduction {
display: flex;
font-size: 24px;
font-weight: 400;
line-height: 40px;/* 这里是文本行高 */
color: #999999;
.text {
position: relative;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2; /* 这里是超出几行省略 */
overflow: hidden;
&:before {
content: "";
float: right;
width: 0;
height: 100%;
margin-bottom: -40px;/* 这里是文本行高的负值 */
}
.btn {
float: right;
clear: both;
color: #6f717e;
&:after {
content: "展开";
width: 120px;/* 这里是展开/收起部分的宽度(可用来隔开文本与展开/收起)(好像实现不了,待修改) */
text-align: right;
}
}
}
#exp {
display: none;
}
#exp:checked + .text {
-webkit-line-clamp: 999;
}
#exp:checked + .text .btn::after {
content: "收起";
}
}
js
//vue2
mounted() {
// 当文本未超出时,不显示展开、收起
if (this.$refs.text.scrollHeight == this.$refs.text.clientHeight) {
this.$refs.btn.style.display = "none";
}
},
效果
待