<style>
.contact { width:100px; height:30px; line-height:30px; background:#ccc; text-align:center; cursor:pointer; }
.tooltip { display:none; position:absolute; top:40px; left:0; width:200px; padding:10px; background:#0099ff; color:#fff; border-radius:4px; }
</style>
<div class="contact">聯系我們</div>
<div class="tooltip">提示內容!提示內容!提示內容!</div>
<script>
$('.contact').hover(
function() { $('.tooltip').show('slow'); },
function() { $('.tooltip').hide('slow'); }
);
</script>
缺點:鼠標快速進出會導致動畫排隊、閃爍;提示框位置固定,不跟隨觸發元素。
$.fn.hoverTooltip = function(options) {
var opts = $.extend({
content: '', // 提示內容,支持選擇器/字符串/函數
delay: 150, // 顯示延遲(ms)
hideDelay: 50, // 隱藏延遲
offsetX: 10, // 水平偏移
offsetY: 10, // 垂直偏移
followMouse: false, // 是否跟隨鼠標移動
zIndex: 9999
}, options);
return this.each(function() {
var $trigger = $(this);
var $tip = null;
var showTimer, hideTimer;
function createTip() {
if ($tip) return;
var content = typeof opts.content === 'function' ? opts.content.call(this) :
(opts.content instanceof $ ? opts.content.clone() : $('').html(opts.content));
$tip = $('')
.append(content)
.css({ position: 'absolute', zIndex: opts.zIndex, display: 'none' })
.appendTo('body');
}
function positionTip(e) {
if (!$tip) return;
var left = (e ? e.pageX : $trigger.offset().left + $trigger.outerWidth()) + opts.offsetX;
var top = (e ? e.pageY : $trigger.offset().top) + opts.offsetY;
// 邊界檢測:防止超出視口
var vpW = $(window).width(), vpH = $(window).height();
var tipW = $tip.outerWidth(), tipH = $tip.outerHeight();
if (left + tipW > vpW) left = vpW - tipW - 10;
if (top + tipH > vpH) top = vpH - tipH - 10;
$tip.css({ left: left, top: top });
}
$trigger.on('mouseenter', function(e) {
clearTimeout(hideTimer);
showTimer = setTimeout(function() {
createTip();
positionTip(e);
$tip.fadeIn(200);
if (opts.followMouse) {
$trigger.on('mousemove.tooltip', positionTip);
}
}, opts.delay);
});
$trigger.on('mouseleave', function() {
clearTimeout(showTimer);
$trigger.off('mousemove.tooltip');
hideTimer = setTimeout(function() {
if ($tip) $tip.fadeOut(200, function() { $tip.remove(); $tip = null; });
}, opts.hideDelay);
});
// 提示框本身也保持顯示(鼠標移入提示框不消失)
$(document).on('mouseenter', '.hover-tooltip', function() { clearTimeout(hideTimer); });
$(document).on('mouseleave', '.hover-tooltip', function() { $trigger.trigger('mouseleave'); });
});
};
// 使用示例
$('.contact').hoverTooltip({
content: '動態生成的提示內容:' + new Date().toLocaleTimeString(),
followMouse: true
});
CSS 樣式建議
.hover-tooltip {
max-width: 300px; padding: 10px 12px; background: #333; color: #fff;
border-radius: 6px; font-size: 13px; line-height: 1.5; box-shadow: 0 4px 12px rgba(0,0,0,.15);
}
.hover-tooltip::before { /* 三角箭頭 */
content: ''; position: absolute; top: -6px; left: 15px;
border: 6px solid transparent; border-bottom-color: #333;
}
常見坑與解決
- 動畫排隊:用
.stop(true, true) 或定時器防抖(如上)。
- z-index 被遮擋:提示框
appendTo('body') 並設置高 z-index。
- 觸發元素動態生成:用事件委託
$(document).on('mouseenter', '.trigger', ...)。
- 移動端無 hover:改用
click 切換,或檢測觸摸設備降級。
總結
從簡單的 hover(show, hide) 到生產級 hoverTooltip 插件,關鍵點在於:防抖動延遲、動態定位與邊界檢測、提示框自身保持顯示、動態內容支持。封裝為插件後可復用於表單驗證提示、圖標說明、預覽卡片等多種場景。
- 上一篇程序編程: jQuery 實現平滑展開/收合面板(Accordion 效果)
- 下一篇程序編程: jQuery 九宮格面板滑動切換:全方位動畫導航實現