抱歉!该站点已经被管理员停止运行,请联系管理员了解详情!

Tooltip 遮挡问题解决方案

Tooltip 被遮挡问题与解决方案

问题展示

Tooltip 被父容器遮挡

这个容器设置了 overflow: hidden 和固定高度,导致内部的 tooltip 被遮挡。

网站 链接
Google
https://www.google.com https://www.google.com
Facebook
https://www.facebook.com https://www.facebook.com

尝试将鼠标悬停在链接上,可以看到 tooltip 被容器裁剪了。

解决方案

使用不同的方法避免遮挡

这个容器使用了解决方案,tooltip 可以正常显示。

网站 链接
Google
https://www.google.com https://www.google.com
Facebook
https://www.facebook.com https://www.facebook.com

尝试将鼠标悬停在链接上,tooltip 可以完整显示。

为什么 Tooltip 会被遮挡?如何解决?

常见原因

解决方案

// 使用JavaScript将tooltip添加到body末尾的示例 document.querySelectorAll('.tooltip').forEach(tooltip => { tooltip.addEventListener('mouseenter', function(e) { const text = this.getAttribute('data-tooltip'); const tooltipElement = document.createElement('div'); tooltipElement.className = 'global-tooltip'; tooltipElement.textContent = text; document.body.appendChild(tooltipElement); // 定位逻辑 const rect = this.getBoundingClientRect(); tooltipElement.style.left = rect.left + 'px'; tooltipElement.style.top = (rect.top - tooltipElement.offsetHeight - 10) + 'px'; }); tooltip.addEventListener('mouseleave', function() { const tooltipElement = document.querySelector('.global-tooltip'); if (tooltipElement) { document.body.removeChild(tooltipElement); } }); });