文本溢出時(shí),顯示“展開--收起”

需求:文本超出4行以后,顯示展開--收起按鈕。例子中使用的v-html
效果
1、默認(rèn)顯示樣式

image.png

2、點(diǎn)擊展開后效果
image.png

vue文件實(shí)現(xiàn)代碼
export default({
  data(){
    return {
      infoScrollHeit: 0, //文本區(qū)域包含滾動(dòng)條高度
      infoClientHeit: 0, //文本區(qū)域不包含滾動(dòng)條高度
    },
    mounted(){
      this.infoScrollHeit = this.$refs.info.scrollHeight
      this.infoClientHeit = this.$refs.info.clientHeight
      // 一下代碼可以將文本還原為:默認(rèn)顯示狀態(tài)
      // if(this.$refs.exp2.checked) {
          // this. infoClientHeit =  80 //4行文本的高度為80px
          // this.$refs.exp2.checked = false
       //}
    }
  }
})
<div class="wrapper">
  <input id="exp2" ref="exp2"  class="exp"  type="checkbox">
  <div class="text">
    <label v-if="infoScrollHeit > infoClientHeit" class="btn" for="exp2"></label>
    <div ref='info' v-html="info"></div>
   </div>
</div>
css文件實(shí)現(xiàn)代碼
.wrapper {
  padding: 8px;
  background: #FEF8E7;
  border-radius: 2px;
  display: flex;
  overflow: hidden;
}
.text {
  font-size: 12px;
  line-height: 20px;
  font-weight: 400;
  color: #FF9505;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  position: relative;
  width: 100%;
}
.text::before {
  content: '';
  height: calc(100% - 20px);
  float: right;
}
.text::after {
  content: '';
  width: 999vw;
  height: 999vw;
  position: absolute;
  box-shadow: inset calc(100px - 999vw) calc(30px - 999vw) 0 0 #FEF8E7;
  margin-left: -100px;
}
.btn{
  font-size: 12px;
  line-height: 20px;
  font-weight: 400;
  color: #2B6DE5;
  float: right;
  clear: both;
  cursor: pointer;
  /* margin-top: -30px; */
}
.btn::before{
  content:'展開'
}
.exp{
  display: none;
}
.exp:checked+.text{
  -webkit-line-clamp: 999;
    transition: all .3s ease-in-out;

}
.exp:checked+.text::after{
  visibility: hidden;
}
.exp:checked+.text .btn::before{
  content:'收起'
}

借用復(fù)選框的功能實(shí)現(xiàn),點(diǎn)擊展開+收起的功能。scrollHeight > clientHeight說明文本溢出

以下代碼可以將展開的文本還原為:默認(rèn)顯示狀態(tài)
  if(this.$refs.exp2.checked) {
    this. infoClientHeit =  80 //4行文本的高度為80px
    this.$refs.exp2.checked = false
  }
image.png

image.png
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容