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

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.效果图

相关推荐
Rubin935 分钟前
判断元素在可视区域?用于滚动加载,数据埋点等
前端
爱学习的茄子5 分钟前
AI驱动的单词学习应用:从图片识别到语音合成的完整实现
前端·深度学习·react.js
用户3802258598246 分钟前
使用three.js实现3D地球
前端·three.js
程序无bug8 分钟前
Spring 面向切面编程AOP 详细讲解
java·前端
zhanshuo8 分钟前
鸿蒙UI开发全解:JS与Java双引擎实战指南
前端·javascript·harmonyos
JohnYan8 分钟前
模板+数据的文档生成技术方案设计和实现
javascript·后端·架构
撰卢32 分钟前
如何提高网站加载速度速度
前端·javascript·css·html
10年前端老司机37 分钟前
在React项目中如何封装一个可扩展,复用性强的组件
前端·javascript·react.js
Struggler28140 分钟前
解决setTimeout/setInterval计时不准确问题的方案
前端
sophie旭1 小时前
《深入浅出react开发指南》总结之 10.1 React运行时总览
前端·react.js·源码阅读