返回 jQuery 实例与特效代码列表
弹窗与提示MIT兼容 jQuery 1.12 / 3.8 / 4.1

悬浮提示框

鼠标悬停或键盘聚焦时在元素上方自动定位弹出提示气泡,离开时隐藏。

兼容性说明:全版本兼容(含 slim 精简版)。

在线交互演示(独立沙箱)

$(function () {
  var $tip = $('#tip-bubble');
  $('.tip-stage').on('mouseenter focusin', '.tip-btn', function () {
    var rect = this.getBoundingClientRect();
    $tip.text($(this).data('tip')).show();
    var left = rect.left + rect.width / 2 - $tip.outerWidth() / 2;
    var top = rect.top - $tip.outerHeight() - 8;
    $tip.css({ left: Math.max(8, left), top: Math.max(8, top) });
  }).on('mouseleave focusout', '.tip-btn', function () {
    $tip.hide();
  });
});
不用 jQuery 怎么写?(现代原生 JS / CSS 替代思路)

纯 CSS 利用 [data-tip]::after 伪元素配合 :hover / :focus-visible 即可零 JS 实现基础提示框。

[data-tip]:hover::after {
  content: attr(data-tip);
  position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
}