基于 element-ui 封装的文本 tooltip,超出显示


<template>
  <el-tooltip
    effect="dark"
    :content="content"
    placement="top-end"
    :disabled="isShowTooltip"
    v-bind="$attrs"
    :open-delay="200"
  >
    <div class="text-wrap" @mouseover="onMouseOver">
      <span ref="text">{{ text }}</span>
    </div>
  </el-tooltip>
</template>

<script>
import { Tooltip } from 'element-ui'
export default {
  name: 'text-tooltip',
  props: ['text'],
  components: {
    Tooltip
  },
  data() {
    return {
      isShowTooltip: false,
      content: "",
    };
  },
  methods: {
    onMouseOver(str) {
      // 内容超出,显示文字提示内容
      const tag = this.$refs.text;
      const parentWidth = tag.parentNode.offsetWidth; // 获取元素父级可视宽度
      const contentWidth = tag.offsetWidth; // 获取元素可视宽度
      this.isShowTooltip = contentWidth <= parentWidth;
      // 鼠标悬停后显示的内容
      this.content = this.text;
    }
  },
};
</script>

<style scoped>
.text-wrap {
  width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
</style>

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM