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

相关推荐

  1. Vue2文本展示收起

    2024-04-05 00:16:04       59 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-05 00:16:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-05 00:16:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-05 00:16:04       87 阅读
  4. Python语言-面向对象

    2024-04-05 00:16:04       96 阅读

热门阅读

  1. MySQL常见故障与优化

    2024-04-05 00:16:04       41 阅读
  2. IP知识详解

    2024-04-05 00:16:04       39 阅读
  3. Golang基础-10

    2024-04-05 00:16:04       36 阅读
  4. 大模型日报2024-04-03

    2024-04-05 00:16:04       45 阅读
  5. 软考之零碎片段记录(五)

    2024-04-05 00:16:04       33 阅读
  6. 速盾:cdn节点存储空间怎么管理

    2024-04-05 00:16:04       42 阅读