当前位置: 首页 > news >正文

文本溢出时显示省略号,并在鼠标移入文本时显示 tooltip全内容

情景一:单行文本溢出,实现效果如下(设置了cursor:pointer;小手,一截图就消失了,正常是能看到的)。


<script setup>
const arr =  [{ id: 'math-1', name: '文本内容文本内容文本内容' },{ id: 'math-2', name: '文本内容' },{ id: 'math-2', name: '文本内容文本内容文本内容文本内容文本内容' }]
// 鼠标移入教材时,判断是否超出宽度,超出显示 tooltip
function showTooltip(event, text) {const target = event.target;if (target.scrollWidth > 160) {tooltip.value.visible = true;tooltip.value.text = text;const rect = target.getBoundingClientRect();tooltip.value.style = {position: 'fixed',top: rect.bottom + 'px',left: rect.left + 'px',zIndex: '9999',maxWidth: '300px'}}
}
// 鼠标移出教材时隐藏 tooltip
function hideTooltip() {tooltip.value.visible = false;
}
</script><template>
<ul class="textbooks"><li v-for="item in arr" :key="arr.id"><span class="textbook-name" @mouseenter="showTooltip($event, item.name)"@mouseleave="hideTooltip">{{ item.name }}</span></li>
</ul><!-- tooltip -->
<div v-if="tooltip.visible" class="custom-tooltip" :style="tooltip.style"> {{tooltip.text }}
</div>
</template><style scoped lang="scss">
.textbooks {list-style: none;margin: 8px 0;padding: 0;li {font-size: 16px;position: relative;cursor: pointer;color: #555a68;display: flex;align-items: center;padding: 6px 2px;.textbook-name {// 单行文本超出省略display: inline-block;max-width: 160px;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;vertical-align: middle;cursor: pointer;        }}
}
</style>

情景:两行文本溢出,效果如图所下。


<script setup>
const arr =  [{ id: 'math-1', name: '文本内容文本内容文本内容' },{ id: 'math-2', name: '文本内容' },{ id: 'math-2', name: '文本内容文本内容文本内容文本内容文本内容' }]
// 鼠标移入教材时,判断是否超出宽度,超出显示 tooltip
function showTooltip(event, text) {const target = event.target;const isOverflow = target.scrollHeight > target.clientHeight + 1if (isOverflow) {tooltip.value.visible = true;tooltip.value.text = text;const rect = target.getBoundingClientRect();tooltip.value.style = {position: 'fixed',top: rect.bottom + 'px',left: rect.left + 'px',zIndex: '9999',maxWidth: '150px'}}
}
// 鼠标移出教材时隐藏 tooltip
function hideTooltip() {tooltip.value.visible = false;
}
</script><template>
<ul class="textbooks"><li v-for="item in arr" :key="arr.id"><span class="textbook-name" @mouseenter="showTooltip($event, item.name)"@mouseleave="hideTooltip">{{ item.name }}</span></li>
</ul><!-- tooltip -->
<div v-if="tooltip.visible" class="custom-tooltip" :style="tooltip.style"> {{tooltip.text }}
</div>
</template><style scoped lang="scss">
.textbooks {list-style: none;margin: 8px 0;padding: 0;li {font-size: 16px;position: relative;cursor: pointer;color: #555a68;display: flex;align-items: center;padding: 6px 2px;.textbook-name {//两行文本省略width: 140px;line-height: 1.2; //行与行之间的间隔display: -webkit-box; // 将元素设置为WebKit内核的弹性盒子模型-webkit-box-orient: vertical; // 设置弹性盒子的排列方向为垂直-webkit-line-clamp: 2; //限制显示的文本行数overflow: hidden; // 隐藏超出容器范围的内容line-clamp: 2; //兼容设置box-orient: vertical;text-align: left;        }}
}
</style>

🔧 scrollHeight vs clientHeight 是什么?

属性名意义举例
clientHeight元素实际可见区域的高度(不包含滚动区域)比如你的元素设置了固定高度 40px,那 clientHeight 就是 40
scrollHeight元素实际内容的高度(即使超出了可见区域也会计算进去)如果内容高度是 60px,那么 scrollHeight 就是 60

🧠 所以 scrollHeight > clientHeight 的意思是:元素的内容高度超出了它的可视区域(说明内容被截断了,出现了省略号或需要滚动)。

http://www.dtcms.com/a/294331.html

相关文章:

  • Taint Bug (污点漏洞):
  • 【bug】websocket协议不兼容导致的一个奇怪问题
  • 垃圾回收介绍
  • jenkins 入门指南:从安装到启动的完整教程
  • Selenium是解决了什么问题的技术?
  • web安全 | docker复杂环境下的内网打点
  • Docker 启动 PostgreSQL 主从架构:实现数据同步的高效部署指南
  • VRRP的概念及应用场景
  • 彩色转灰度的核心逻辑:三种经典方法及原理对比
  • 优雅!通过编程方式重启 Spring Boot 应用的 3 种方案
  • Apache PDFBox深入实践
  • python学智能算法(二十九)|SVM-拉格朗日函数求解中-KKT条件
  • PHP语法高级篇(五):回调函数与异常处理
  • Ansible 变量指南:声明、优先级、作用域与最佳实践(一)
  • Jquery、Vue 、Ajax、axios、Fetch区别
  • Ansible在配置管理中的应用
  • ffmpeg rtsp 丢包处理
  • SSH公钥认证连接过程
  • Linux 进程与服务管理~进程基础、进程查看、进程控制、服务管理、开机启动​​
  • 纯CPU场景下C++的分布式模型训练框架设计思路
  • 2025.7.20 文献阅读-基于深度神经网络的半变异函数在 高程数据普通克里格插值中的应用
  • go语言数据结构与排序算法
  • 【C++】C++ 的入门知识2
  • Android 持久化存储原理与使用解析
  • MATLAB | 绘图复刻(二十二)| 带树状图的三角热图合集
  • 个性化网页计数器
  • C 语言介绍
  • 【数据结构】二叉树的链式结构--用C语言实现
  • 嵌入式linux下的NES游戏显示效果优化方案:infoNES显示效果优化
  • 我用EV-21569-SOM评估来开发ADSP-21569(十三)-SigmaStudio Plus做开发(4)