前端超出最大宽度,显示...,鼠标hover显示浮层,显示全部

 子组件

<template>
  <el-tooltip class="tooltip" :disabled="isShowTooltip" :content="text" placement="bottom" effect="light">
    <div class="container" @mouseover="onMouseOver(refName)">
      <span class="ellipsis" :ref="refName">{{ text }}</span>
    </div>
  </el-tooltip>
</template>

<script>
export default {
  data() {
    return {
      isShowTooltip: true,
    }
  },
  props: {
    text: {
      type: String,
      required: true,
    },
    refName: {
      type: String,
      required: true,
    },
  },
  methods: {
    onMouseOver(refName) {
      console.log(refName, this.$refs[refName].parentNode, this.$refs[refName])
      const parentWidth = this.$refs[refName].parentNode.offsetWidth
      const contentWidth = this.$refs[refName].offsetWidth
      // 判断是否开启 tooltip 功能,如果溢出显示省略号,则子元素的宽度势必短于父元素的宽度
      console.log(parentWidth, contentWidth)
      if (contentWidth == parentWidth) {
        this.isShowTooltip = false
      } else {
        this.isShowTooltip = true
      }
    },
  },
}
</script>

<style lang="scss" scoped>
 /* 显示的行数:一行 */
.ellipsis {
  display: inline-block;
  max-width: 100%; /* 定宽 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  /* 显示的行数:两行 */
  // display: -webkit-box;
  // -webkit-box-orient: vertical;
  // -webkit-line-clamp: 2; /* 显示的行数 */
  // overflow: hidden;
  // text-overflow: ellipsis;
}

.container {
  display: flex;
  flex-wrap: nowrap;
  max-width: 100%; /* 定宽 */
  overflow: hidden;
}
</style>

父组件引用

 <toopitnormal ref="reftoopit2" :text="item2.pointName" :refName="refName2"></toopitnormal>

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值