【Vue3 Antdv】Ant Design Vue文字溢出鼠标滑上显示tooltip。不溢出,鼠标滑上不显示tooltip

博客主要介绍了VUE2和VUE3的组件封装代码及使用方法,涵盖了前端开发中VUE不同版本的关键应用内容,为开发者提供了相关技术参考。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

VUE3组件封装代码

<template>
  <a-tooltip @mouseenter="showToolTip" v-bind="getBindValue">
    <template #title>
      {{ props.title }}
    </template>
    <slot><span>{{ props.title }}</span></slot>
  </a-tooltip>
</template>

<script setup>
import {defineProps,computed,useAttrs} from "vue";

const props = defineProps({
  title:{ //提示标题
    type:String,
    default:''
  }
})

/** 获取绑定属性 */
const getBindValue = computed(() => {
  const delArr = ['title']  //需要过滤的属性
  const attrs = useAttrs()
  const obj = { ...attrs, ...props }
  for (const key in obj) {
    if (delArr.indexOf(key) !== -1) {
      delete obj[key]
    }
  }
  return obj
})

/** 不溢出不显示tooltip */
const showToolTip = (e) => {
  const {clientWidth,scrollWidth} = e.target
  if (clientWidth >= scrollWidth) {
    e.target.style.pointerEvents = "none"; // 阻止鼠标事件
  }
}
</script>

<style scoped>

</style>

VUE3使用

<template>
	<ToolTip :title="text" placement="topLeft">
		<span class="text">{{text}}</span>
	</ToolTip>
</template>

<style>
span.text{
  display: inline-block;
  width: 100%;
  /* 这是单行溢出显示...的样式 */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space:nowrap;
}
</style>

VUE2组件封装代码

<template>
  <a-tooltip @mouseenter="showToolTip" v-bind="getBindValue">
    <template #title>
      {{ title }}
    </template>
    <slot><span class="text" :style="{maxWidth:maxWidth}">{{ title }}</span></slot>
  </a-tooltip>
</template>

<script>
/**
 * @description 用于设置单行溢出隐藏的组件 不溢出不做提示
 * @params title:表格文本内容和鼠标悬停内容  maxWidth:最大宽度,超出此宽度隐藏多余文本显示...
**/
export default {
  name: "ToolTip",
  props: {
    title:{ //提示标题
      type:String,
      default:''
    },
    maxWidth:{  // 溢出隐藏的宽度
      type:String,
      default: '100%'
    }
  },
  computed:{
    /** 获取绑定属性 */
    getBindValue(){
      const delArr = ['title','maxWidth']  //需要过滤的属性
      const attrs = this.$attrs
      const obj = { ...attrs, ...this.$props }
      for (const key in obj) {
        if (delArr.indexOf(key) !== -1) {
          delete obj[key]
        }
      }
      return obj
    }
  },
  methods:{
    /** 不溢出不显示tooltip */
    showToolTip(e) {
      const {clientWidth,scrollWidth} = e.target
      if (clientWidth >= scrollWidth) {
        e.target.style.pointerEvents = "none"; // 阻止鼠标事件
      }
    }
  }
}
</script>
<style scoped>
span.text{
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
</style>

VUE2使用

<ToolTip :title="text" placement="topLeft" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

孑然R

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值