基于elementui二次封装tooltip组件

javascript 复制代码
<template>
  <div class="text-tooltip">
    <el-tooltip :content="content" :disabled="isShowTooltip" class="item" effect="dark" placement="bottom">
      <div class="tooltip_over">
        <slot class="label" name="label"></slot>
        <div :class="className" :style="{color:normal?'#999':'','font-weight':normal?'normal':''}" class="over-flow"
             @mouseover="onMouseOver(refName)">
          <span :ref="refName">{{ content }}</span>
        </div>
      </div>
    </el-tooltip>
  </div>
</template>

<script>
export default {
  name: 'textTooltip',
  props: {
    //文字样式是否使用默认
    normal: {
      type: Boolean,
      default: true
    },
    // 显示的文字内容
    content: {
      type: String,
      default: ""
    },
    // 外层框的样式,在传入的这个类名中设置文字显示的宽度
    className: {
      type: String,
      default: ""
    },
    // 为页面文字标识(如在同一页面中调用多次组件,此参数不可重复)
    // 当同一页面使用多次组件时,需要定义不同的refName属性
    refName: {
      type: String,
      default: ""
    },
    //若出现外层框数值不确定的情况,可使用传参的方式定义父级的宽度
    parentWidth: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      isShowTooltip: true
    }
  },
  methods: {
    onMouseOver(str) {
      let contentWidth = this.$refs[str].offsetWidth;
      if (this.parentWidth != 0) {
        if (this.parentWidth < contentWidth + 20) {
          this.isShowTooltip = false;
        } else {
          this.isShowTooltip = true;
        }
      } else {
        let parentWidth = this.$refs[str].parentNode.offsetWidth;
        console.log(contentWidth,parentWidth,'contentWidth')
        // 判断是否开启tooltip功能
        if (parentWidth != contentWidth) {
          this.isShowTooltip = false;
        } else {
          this.isShowTooltip = true;
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
@import '@/styles/mixin.scss';

.tooltip_over {
  display: flex;

  .label {
    width: 70px;
  }

  .over-flow {
    flex: 1;
    @include ellipsis_more(1);
    word-break: break-all;
  }
}
</style>

使用:

javascript 复制代码
<Tooltip
                  :content="mainData.courseContent || '暂无'"
                  refName="tooltipOverCourseContent"
              >
                <label slot="label">课程简介:</label>
              </Tooltip>
相关推荐
Nejosi_念旧9 分钟前
Vue API 、element-plus自动导入插件
前端·javascript·vue.js
麻芝汤圆1 小时前
MapReduce 入门实战:WordCount 程序
大数据·前端·javascript·ajax·spark·mapreduce
Peter 谭3 小时前
React Hooks 实现原理深度解析:从基础到源码级理解
前端·javascript·react.js·前端框架·ecmascript
周胡杰4 小时前
鸿蒙接入flutter环境变量配置windows-命令行或者手动配置-到项目的创建-运行demo项目
javascript·windows·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
乌夷5 小时前
axios结合AbortController取消文件上传
开发语言·前端·javascript
wuyijysx7 小时前
JavaScript grammar
前端·javascript
学渣y9 小时前
React状态管理-对state进行保留和重置
javascript·react.js·ecmascript
_龙衣9 小时前
将 swagger 接口导入 apifox 查看及调试
前端·javascript·css·vue.js·css3
struggle202510 小时前
continue通过我们的开源 IDE 扩展和模型、规则、提示、文档和其他构建块中心,创建、共享和使用自定义 AI 代码助手
javascript·ide·python·typescript·开源
x-cmd10 小时前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
前端·javascript·windows·npm·node.js