【vue/js】文字超长悬停显示的几种方式

方法一:::after伪元素+content

  1. CSS 修改
    样式添加悬停提示效果,当鼠标悬停在文本上时显示完整内容:
js 复制代码
.grid-item-value {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60%;
  user-select: text;
  position: relative;
  
  // 创建伪元素作为工具提示
  &::after {
    content: attr(data-full-text);  // 使用 data-full-text 属性的内容
    position: absolute;
    bottom: 100%;                   // 显示在元素上方
    left: 50%;
    transform: translateX(-50%);
    background-color: #333;         // 深色背景
    color: white;                   // 白色文字
    padding: 4px 8px;               // 内边距
    border-radius: 4px;             // 圆角
    white-space: normal;            // 允许换行
    word-break: break-all;          // 强制换行
    max-width: 200px;               // 最大宽度
    z-index: 100;                   // 确保显示在最上层
    opacity: 0;                     // 默认隐藏
    pointer-events: none;           // 不响应鼠标事件
    transition: opacity 0.2s;       // 渐显动画
  }
  
  // 悬停时显示完整文本
  &:hover::after {
    opacity: 1;
  }
}
  1. 模板修改
    更新模板代码,将完整文本存储在 data-full-text 属性中:
html 复制代码
<div 
  class="grid-item-value" 
  :data-full-text="consoleStore.activeRobot[item.value] == undefined ? '--' : consoleStore.activeRobot[item.value]"
>
  {{
    consoleStore.activeRobot[item.value] == undefined
      ? '--'
      : consoleStore.activeRobot[item.value]
  }}
</div>

你说得对,当前的定位确实有问题。现在的 tooltip 是相对于整个 .grid-item-value 元素定位的,但我们需要让它相对于父容器(.grid-info.content)正确定位。让我帮你调整一下实现方式。

有两种更好的解决方案:

方法二:使用 Element Plus 的 Tooltip 组件

html 复制代码
<template>
  <div v-if="props.pannel.type === 'robot'" class="grid-info">
    <div v-for="(item, index) in consoleStore.robotData" :key="index" class="grid-item">
      <div class="grid-item-title">{{ item.title }}</div>
      <el-tooltip 
        :content="consoleStore.activeRobot[item.value] == undefined ? '--' : consoleStore.activeRobot[item.value]" 
        placement="top"
        :disabled="!consoleStore.activeRobot[item.value] || consoleStore.activeRobot[item.value].length <= 10"
      >
        <div class="grid-item-value">
          {{
            consoleStore.activeRobot[item.value] == undefined
              ? '--'
              : consoleStore.activeRobot[item.value]
          }}
        </div>
      </el-tooltip>
    </div>
  </div>
</template>

方法三:使用原生 HTML title 属性(最简单)

html 复制代码
<div 
  class="grid-item-value" 
  :title="consoleStore.activeRobot[item.value] == undefined ? '--' : consoleStore.activeRobot[item.value]"
>
  {{
    consoleStore.activeRobot[item.value] == undefined
      ? '--'
      : consoleStore.activeRobot[item.value]
  }}
</div>

大家可以根据自身项目情况,选择最合适方案。

相关推荐
pas1363 分钟前
42-mini-vue 实现 transform 功能
前端·javascript·vue.js
柒.梧.25 分钟前
从零搭建SpringBoot+Vue+Netty+WebSocket+WebRTC视频聊天系统
vue.js·spring boot·websocket
你的代码我的心30 分钟前
微信开发者工具开发网页,不支持tailwindcss v4怎么办?
开发语言·javascript·ecmascript
esmap30 分钟前
OpenClaw与ESMAP AOA定位系统融合技术分析
前端·人工智能·计算机视觉·3d·ai·js
毕设源码-钟学长38 分钟前
【开题答辩全过程】以 基于node.js vue的点餐系统的设计与实现为例,包含答辩的问题和答案
前端·vue.js·node.js
努力d小白38 分钟前
leetcode438.找到字符串中所有字母异位词
java·javascript·算法
小白路过40 分钟前
记录vue-cli-service serve启动本地服务卡住问题
前端·javascript·vue.js
We་ct44 分钟前
LeetCode 1. 两数之和:两种高效解法(双指针 + Map)
前端·算法·leetcode·typescript·哈希算法
LYFlied1 小时前
边缘智能:下一代前端体验的技术基石
前端·人工智能·ai·大模型
1024小神1 小时前
用css的clip-path裁剪不规则形状的图片展示
前端·css