文字超出收起展开功能的实现(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.效果图

相关推荐
CappuccinoRose11 分钟前
React框架学习文档(七)
开发语言·前端·javascript·react.js·前端框架·reactjs·react router
FFF-X21 分钟前
前端字符串模糊匹配实现:精准匹配 + Levenshtein 编辑距离兜底
前端
Hi_kenyon31 分钟前
Ref和Reactive都是什么时候使用?
前端·javascript·vue.js
小小测试开发33 分钟前
UI自动化测试:CSS定位方式超详细解析(附实战示例)
css·ui·tensorflow
止观止1 小时前
深入理解 interface vs type:终结之争
前端·typescript
css趣多多1 小时前
vue环境变量
前端
RFCEO1 小时前
前端编程 课程十五、:CSS核心基础3:文字+段落样式
前端·css·文字+段落样式·css文本样式·美化页面文本内容·演示动画说明·单行文字垂直居中技
摇滚侠1 小时前
【程序员入门系列】jQuery 零基础入门到精通!Jquery 选择器 API
前端·javascript·jquery
im_AMBER1 小时前
Leetcode 111 两数相加
javascript·笔记·学习·算法·leetcode
源力祁老师1 小时前
深入解析 Odoo 中 default_get 方法的功能
java·服务器·前端