封装组件实现容器文字过长显示省略号 + 鼠标移入显示提示框+自动判断文字长度是否显示提示框

本文介绍了如何在Vue项目中使用CSS样式和el-tooltip组件,实现在内容过长时自动判断并显示提示框,同时支持自定义内容展示。
摘要由CSDN通过智能技术生成

效果图

1.文字过长显示省略号

可以用css设置 常用的3个属性 注意要设置宽度

<template>
  <div class="content" :style="{width: props.width}">
    {{props.content}}
  </div>
</template>
<script setup>

  const props = defineProps({
     content: { type: String, default: "" },
  width: { type: String, default: "" },
  }) 
</script>
<style>
  .content {
    overflow: hidden; 
    white-space: nowrap;
    text-overflow: ellipsis
  }
</style>

2.鼠标移入显示提示框

鼠标移入显示提示框可以自己写一个, 也一个直接用组件。 这里直接用element中的el-tooltip组件。

<template>
  <el-tooltip
    effect="dark"
    :content="props.content"
    placement="top"
  >
    <div class="content" :style="{width: props.width}">
      {{props.content}}
    </div>
  </el-tooltip>
</template>
<script setup >
  const props = defineProps({
  content: { type: String, default: "" },
  width: { type: String, default: "" },
  }) 

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

3.自动判断文字长度是否显示提示框

判断内容的宽度是否超过了父级容器的宽度

  <template>
    <el-tooltip
      effect="dark"
      :content="props.content"
      placement="top"
      :disabled="isShow"
    >
      <div class="content" :style="{width: props.width}" @mouseover="isShowTooltip">
        <span ref="contentRef">{{props.content}}</span>
      </div>
    </el-tooltip>
  </template>
  <script setup >
    import { ref } from 'vue'
 const props = defineProps({
  content: { type: String, default: "" },
  width: { type: String, default: "" },
  }) 
    // 使用isShow来控制tooltip是否显示
    let isShow = ref(true)
    // 在span标签上定义一个ref
    const contentRef  = ref()

//鼠标移入容器的时候进行判断
    const isShowTooltip = function () {
      if(contentRef.value.parentNode.offsetWidth > contentRef.value.offsetWidth) {
        isShow.value = true
      } else {
        isShow.value = false
      }
    }
  </script>
  <style>
    .content {
      overflow: hidden; 
      white-space: nowrap;
      text-overflow: ellipsis
    }
  </style>

4.更多扩展可以使用solt,  进行自定义显示tooltip内容, 也可以显示非文本内容

 <template>
    <el-tooltip
      effect="dark"
      :content="props.tooltipContent ? props.tooltipContent : props.content"
      placement="top"
      :disabled="isShow"
    >
      <template #content>
        <!-- 此处的默认值先看tooltipContent有没有,没有就给默认content -->
        <slot name="tooltipContent">{{props.tooltipContent ? props.tooltipContent : props.content}}</slot>
      </template>
      <div class="content" :style="{width: props.width}" @mouseover="isShowTooltip">
        <span ref="contentRef">
          <!-- 给一个没有写插槽的默认值,兼容纯文本的情况 -->
          <slot name="content">{{props.content}}</slot>
        </span>
      </div>
    </el-tooltip>
  </template>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值