当前位置: 首页 > article >正文

使用el-tooltip封装省略号组件内容超出显示tooltip

在公共组件中封装el-tooltip的显示,当传入的内容超出父元素的大小时,显示tooltip组件

第一种方式(单行)

组件封装

<template>
  <div class="tooltip-container">
    <el-tooltip class="my-tooltip" :disabled="showTooltip" :content="text">
      <p ref="tooltipBox" class="text-box">
        <span ref="tooltipItem" class="">{{ text }}</span>
      </p>
    </el-tooltip>
  </div>
</template>

<script>
export default {
  name: 'MyTooltip',
  props: {
    text: {
      type: String,
      default: () => ''
    }
  },
  data () {
    return {
      showTooltip: true
    }
  },
  watch: {
    text: {
      handler () {
        this.$nextTick(() => this.checkWidth())
      },
      immediate: true
    }
  },
  methods: {
    checkWidth () {
      const boxWidth = this.$refs.tooltipBox.offsetWidth
      const itemWidth = this.$refs.tooltipItem.offsetWidth
      this.showTooltip = boxWidth > itemWidth
    }
  }
}
</script>
<style scoped lang="scss">
.tooltip-container {
  width: 100%;

  .text-box {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}
</style>

组件使用

<template>
  <div class="test">
    <MyTooltip :text="'超出部分显出部分显出部超出部分显出部分显出部超出部分显出部分显出部'"></MyTooltip>
  </div>
</template>

<script>
import MyTooltip from '@/components/MyTooltip'
export default {
  name: 'demoPage2',
  components: {
    MyTooltip
  },
  data () {
    return {
    }
  },
  methods: {
  }
}
</script>

<style lang="scss">
.test{
  width:150px;
  cursor:pointer;
}
</style>

显示效果

在这里插入图片描述

第二种方式(单行)

组件封装

<template>
  <el-tooltip effect="light" :content="content" placement="top" :disabled="isDisabled">
    <div class="cm-tooltip" @mouseover="mouseoverHandle">
      <span ref="contentRef">{{ content }}</span>
    </div>
  </el-tooltip>
</template>

<script>
export default {
  name: 'MyTooltip',
  props: {
    content: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      isDisabled: false
    }
  },
  methods: {
    mouseoverHandle () {
      const parentWidth = this.$refs.contentRef.parentNode.offsetWidth
      const width = this.$refs.contentRef.offsetWidth
      this.isDisabled = width <= parentWidth
      console.log(this.isDisabled, parentWidth, width)
    }
  }
}
</script>
<style scoped lang="scss">
.cm-tooltip{
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>

组件使用

<template>
  <div class="test">
    <MyTooltip :content="'超出部分显出部分显出部超出部分显出部分显出部超出部分显出部分显出部'"></MyTooltip>
  </div>
</template>

<script>
import MyTooltip from '@/components/MyTooltip2'
export default {
  name: 'demoPage2',
  components: {
    MyTooltip
  },
  data () {
    return {
    }
  },
  methods: {
  }
}
</script>

<style lang="scss">
.test{
  width:150px;
  cursor:pointer;
}
</style>

显示效果

在这里插入图片描述

第三种方式(多行)

组件封装

<template>
  <el-tooltip effect="light" :content="content" placement="top" :disabled="isDisabled">
    <div class="cm-tooltip" @mouseover="mouseoverHandle">
      <span ref="contentRef">{{ content }}</span>
    </div>
  </el-tooltip>
</template>

<script>
export default {
  name: 'MyTooltip',
  props: {
    content: {
      type: [String, Number],
      required: true
    }
  },
  data () {
    return {
      isDisabled: false
    }
  },
  methods: {
    mouseoverHandle () {
      const parentHeight = this.$refs.contentRef.parentNode.offsetHeight
      const height = this.$refs.contentRef.offsetHeight
      this.isDisabled = height <= parentHeight
    }
  }
}
</script>
<style scoped lang="scss">
.cm-tooltip{
  display: -webkit-box;
  overflow: hidden;
  text-overflow: ellipsis;
  -webkit-line-clamp: 2;
  line-break: anywhere;
  -webkit-box-orient: vertical;
}
</style>

组件使用

<template>
  <div class="test">
    <MyTooltip :content="'超出部分显出部分显出部超出部分显出部分显出部超出部分显出部分显出部'"></MyTooltip>
  </div>
</template>

<script>
import MyTooltip from '@/components/MyTooltip2'
export default {
  name: 'demoPage2',
  components: {
    MyTooltip
  },
  data () {
    return {
    }
  },
  methods: {
  }
}
</script>

<style lang="scss">
.test{
  width:150px;
  cursor:pointer;
}
</style>

显示效果

在这里插入图片描述


http://www.kler.cn/a/591172.html

相关文章:

  • 基于Android语言实现身份证二要素核验-身份证实名认证API
  • vscode使用ssh同时连接主机CentOS:user和ubuntu20.04:docker
  • Canary
  • 堆排序:力扣215.数组中的第K个大元素
  • 使用React和google gemini api 打造一个google gemini应用
  • RustDesk自建远程桌面服务教程
  • 蓝桥杯练习day1:自除数
  • 深入理解 C 语言中的 scanf、printf
  • 《算法笔记》9.2小节——数据结构专题(2)->二叉树的遍历 问题 D: 二叉树遍历
  • 受控组件非受控组件
  • 新造车不再比拼排名,恰是曲终人散时,剩者为王
  • 【大语言模型知识】Transformer架构概述
  • LLVM学习-- 构建和安装
  • redis的典型应用 --缓存
  • 自定义捕捉与处理信号的底层逻辑
  • mkdir /path/aa/bb与mkdir -p /path/aa/bb的区别
  • 案例5_3: 6位数码管静态显示
  • Maven | 站在初学者的角度配置
  • 【写作科研化】LongWriter: Unleashing 10,000+ Word Generation From Long Context LLMs
  • Hard Disk Sentinel:您的硬盘健康“全科医生”,守护数据安全的智能管家