前端长文本展开收起


<template>
  <div :class="showTotal ? 'total-introduce' : 'detailed-introduce'">
    <div class="intro-content" :title="introduce" ref="desc">
      <span class="merchant-desc" v-if="introduce">{{ introduce }}</span>
      <div class="unfold" @click="showTotalIntro" v-if="showExchangeButton">
        <p>{{ exchangeButton ? "展开" : "收起" }}</p>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      introduce:
        "1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊1aaaaAA啊啊啊啊啊", // 简介
      showTotal: true, // 简介全部显示
      exchangeButton: false, // 展开/收起文字改变
      showExchangeButton: false, // 是否显示 展开/收起 文字
    };
  },
  created() {
    this.$nextTick(() => {
      debugger
      if (!this.$refs.desc) {
        return;
      }
      const descHeight = window
        .getComputedStyle(this.$refs.desc)
        .height.replace("px", "");
      if (parseInt(descHeight) * 2 > 139) {
        this.showExchangeButton = true;
        this.exchangeButton = true;
        this.showTotal = false;
      } else {
        this.showExchangeButton = false;
        this.showTotal = true;
      }
    });
  },
  methods: {
    showTotalIntro() {
      this.showTotal = !this.showTotal;
      this.exchangeButton = !this.exchangeButton;
    },
  },
};
</script>

<style lang="less" scoped>
.total-introduce {
  height: auto;
  overflow: hidden;
  color: #666;
  margin-top: 30px;
  .intro-content {
    .merchant-desc {
      width: 100%;
    }
  }
  .unfold {
    display: block;
    float: right;
    width: 80px;
    height: 40px;
    z-index: 8;
    text-align: center;
    p {
      margin: 0;
      line-height: 40px;
      color: #0098ff;
    }
  }
}
.detailed-introduce {
  color: #666;
  position: relative;
  overflow: hidden;
  margin-top: 30px;
  .intro-content {
    //最大高度设为3倍的行间距
    max-height: 50px;
    word-wrap: break-word;
    word-break: break-all;
    background-color: #fff;
    color: #fff;
    overflow: hidden;
    .merchant-desc {
      width: 100%;
      line-height: 40px;
    }
    &::after,
    &::before {
      content: attr(title);
      position: absolute;
      left: 0;
      top: 0;
      width: 100%;
      color: #666;
    }
    &::before {
      display: block;
      overflow: hidden;
      z-index: 1;
      max-height: 80px;
      background: #fff;
    }
    &::after {
      display: -webkit-box;
      -webkit-box-orient: vertical;
      height: 120px;
      // 截取行数
      -webkit-line-clamp: 3;
      text-overflow: ellipsis;
      box-sizing: border-box;
      // 行首缩进字符数,值为[(截取行数-1)*尾部留空字符数]
      text-indent: -6em;
      // 尾部留空字符数
      padding-right: 3em;
    }
    .unfold {
      z-index: 8;
      width: 80px;
      // height: 40px;
      outline: 0;
      position: absolute;
      right: 0;
      bottom: 0;
      text-align: center;
      p {
        margin: 0;
        color: #0098ff;
      }
    }
  }
}
</style>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值