文字超出收起展开功能的实现(vue2)

本文介绍了如何在Vue中使用TextClamp组件来创建可展开和收起的文本显示,包括模板结构、props设置、计算高度以及使用MutationObserver监听内容变化。
摘要由CSDN通过智能技术生成

1.编写展开收起组件

 
 <template>
    <div class="text-clamp">
      <div class="text" :style="{height}">
        <span v-if="isVisible" class="btn" @click="toggle">{{isExpand ? '收起' : '... 展开'}}</span>
        <div ref="textRef" :style="commonStyle">
          <slot />
        </div>
      </div>
    </div>
  </template>
   
  <script>
  export default {
    name: "TextClamp",
    props: {
      fontSize: {
        type: Number,
        default: 14
      },
      lines: {
        type: Number,
        default: 1
      },
      lineHeight: {
        type: Number,
        default: 20
      },
      selectors: {
        type: String,
        default: ""
      }
    },
    data () {
      return {
        isExpand: false,
        isVisible: false,
        textHeight: 0
      };
    },
    computed: {
      height () {
        if (this.isExpand) {
          return this.$refs.textRef.clientHeight + 'px';
        } else {
          return Math.min((this.lines * this.lineHeight), this.textHeight) + 'px';
        }
      },
      commonStyle () {
        return {
          lineHeight: this.lineHeight + 'px',
          fontSize: this.fontSize + 'px',
        };
      }
    },
    mounted () {
      this.init();
      // 监听插槽变化
      const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
          if (mutation.type === "characterData") {
            this.init();
          }
        });
      });
      observer.observe(this.$refs.textRef, {
        characterData: true,
        subtree: true,
        childList: true
      });
    },
    methods: {
      init () {
        this.isExpand = false;
        this.textHeight = this.$refs?.textRef?.clientHeight || 0;
        this.isVisible = this.textHeight > this.lines * this.lineHeight;
      },
      toggle () {
        this.isExpand = !this.isExpand;
        if (!this.isExpand && this.selectors) {
          const initEl = document.querySelector(this.selectors);
          setTimeout(() => {
            initEl.scrollIntoView({
              behavior: 'smooth',
              block: 'start',
              inline: 'center'
            });
          }, 97);
        }
      }
    }
  };
  </script>
   
  <style lang="scss" scoped>
  .text-clamp {
    display: flex;
    overflow: hidden;
  }
  .text {
    font-size: 20px;
    transition: 0.3s height;
  }
  .text::before {
    content: "";
    height: calc(100% - 20px);
    float: right;
  }
  .btn {
    float: right;
    clear: both;
    font-size: 12px;
    line-height: 14px;
    padding: 2px 6px;
    background: #1890ff;
    border-radius: 2px;
    color: #fff;
    cursor: pointer;
  }
  </style>

2.导入并注册组件

import MyText from './textOver.vue';
export default {
    components: {
        MyText
    },
}

3.使用组件

 <MyText :lines="2">{{ showText }}</MyText>

4.效果图

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值