基于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>
相关推荐
Glommer5 分钟前
某音 Js 逆向思路
javascript·逆向
街尾杂货店&9 分钟前
webpack - 单独打包指定JS文件(因为不确定打出的前端包所访问的后端IP,需要对项目中IP配置文件单独拿出来,方便运维部署的时候对IP做修改)
前端·javascript·webpack
月光技术杂谈10 分钟前
用Deepseek 实现一个基于web的扣图应用
前端·javascript·html5·ccs·tensorflow.js·canvas api
kevlin_coder1 小时前
🚀 实现同一个滚动区域包含多个虚拟滚动列表
前端·javascript
金梦人生1 小时前
JS 性能优化
前端·javascript
peachSoda71 小时前
自定义配置小程序tabbar逻辑思路
javascript·vue.js·微信小程序·小程序
hbqjzx1 小时前
记录一个自动学习的脚本开发过程
开发语言·javascript·学习
浪裡遊1 小时前
React开发模式解析:JSX语法与生命周期管理
前端·javascript·react.js·前端框架·ecmascript
fruge1 小时前
Vue 3 完全指南:响应式原理、组合式 API 与实战优化
javascript·vue.js·ecmascript
Aoda2 小时前
从痛点到落地:PawHaven 的 Monorepo 架构设计
前端·javascript