MediaWiki:Gadget-UnihanTooltips.js:修订间差异
跳转到导航
跳转到搜索
删除的内容 添加的内容
小无编辑摘要 |
小 // Edit via Wikiplus |
||
| (未显示同一用户的3个中间版本) | |||
| 第1行: | 第1行: | ||
/* |
/* |
||
本小工具可以將[[Template:僻字]]的提示由原來的title提示改為元素式彈出提示,使觸控式裝置可以觀看有關提示 |
本小工具可以將[[Template:僻字]]的提示由原來的title提示改為元素式彈出提示,使觸控式裝置可以觀看有關提示 |
||
适配说明:兼容MediaWiki 1.43+,修复模块依赖错误,移除jquery.ui.animate依赖,优化动画逻辑 |
|||
*/ |
*/ |
||
// 显式传入jQuery和mediaWiki,避免$冲突 |
|||
(function() { |
|||
(function($, mw) { |
|||
// 使用MediaWiki的Cookie方法 |
|||
"use strict"; |
|||
const dontLoad = mw.util.getParamValue("UTdontload"); |
|||
if (dontLoad && !isNaN(dontLoad)) { |
|||
/*===== 初始化检查:控制工具是否加载 =====*/ |
|||
mw.cookie.set("UTdontload", "1", { path: "/", expires: parseInt(dontLoad) }); |
|||
// 获取URL参数"UTdontload"(0=清除不加载状态,数字=设置不加载状态) |
|||
const dontLoadParam = mw.util.getParamValue("UTdontload"); |
|||
if (dontLoadParam !== null) { |
|||
const paramVal = parseInt(dontLoadParam, 10); |
|||
if (!isNaN(paramVal)) { |
|||
paramVal === 0 ? mw.storage.remove("UTdontload") : mw.storage.set("UTdontload", "1"); |
|||
} |
|||
} |
|||
// 若本地存储存在"不加载"状态,直接终止 |
|||
if (mw.storage.get("UTdontload") === "1") { |
|||
return; |
|||
} |
} |
||
if (mw.cookie.get("UTdontload") === "1") return; |
|||
/*===== 主初始化:命名空间与容器 =====*/ |
|||
(function() { |
|||
const canonicalNamespace = mw.config.get("wgCanonicalNamespace"); |
|||
// 使用MediaWiki提供的content容器 |
|||
const allowedNamespaces = ["", "Project", "Help"]; // 主/项目/帮助命名空间 |
|||
const bodyContent = mw.util.$content || document.body; |
|||
if (!allowedNamespaces.includes(canonicalNamespace)) { |
|||
const canonicalNamespace = mw.config.get('wgCanonicalNamespace'); |
|||
return; |
|||
} |
|||
// 只在这些命名空间运行 |
|||
if (!['', 'Project', 'Help'].includes(canonicalNamespace)) return; |
|||
// 获取内容容器(优先mw-content-text,兼容旧版本) |
|||
// 更可靠的触摸检测 |
|||
const $bodyContent = $("#mw-content-text").length ? $("#mw-content-text") : $("body"); |
|||
const isTouchscreen = window.matchMedia('(hover: none), (pointer: coarse)').matches || |
|||
const bodyContent = $bodyContent[0]; |
|||
mw.config.get('wgDisplayResolution') === 'mobile' || |
|||
'ontouchstart' in document.documentElement; |
|||
const timerLength = isTouchscreen ? 0 : 200; |
|||
/*===== 设备类型检测 =====*/ |
|||
$(".inline-unihan").each(function() { |
|||
const isTouchscreen = window.matchMedia("(hover: none), (pointer: coarse)").matches || |
|||
let tooltipNode = null; |
|||
mw.config.get("wgDisplayResolution") === "mobile" || |
|||
let hideTimer = null; |
|||
"ontouchstart" in document.documentElement; |
|||
let showTimer = null; |
|||
const hoverDelay = isTouchscreen ? 0 : 200; // 触摸设备无延迟 |
|||
/*===== 工具函数:弹窗核心逻辑 =====*/ |
|||
function hide(refLink) { |
|||
/** |
|||
if (tooltipNode && tooltipNode.parentNode === bodyContent) { |
|||
* 初始化元素数据(存储到元素data中) |
|||
hideTimer = setTimeout(() => { |
|||
*/ |
|||
$(tooltipNode).animate({ |
|||
function initElementData(element) { |
|||
opacity: 0 |
|||
let tooltipData = $(element).data("tooltipData"); |
|||
}, 100, function() { |
|||
if (!tooltipData) { |
|||
const originalTitle = element.title || ""; |
|||
tooltipNode.parentNode.removeChild(tooltipNode); |
|||
element.title = ""; // 清空原生title提示 |
|||
$(element).addClass("UHTarget"); // 添加高亮类 |
|||
tooltipData = { |
|||
originalTitle: originalTitle, |
|||
tooltipNode: null, |
|||
hideTimer: null, |
|||
showTimer: null |
|||
}; |
|||
$(element).data("tooltipData", tooltipData); |
|||
} |
|||
return tooltipData; |
|||
} |
|||
/** |
|||
function show() { |
|||
* 创建弹窗内容(支持多行文本分割) |
|||
if (!tooltipNode.parentNode || tooltipNode.parentNode.nodeType === 11) { |
|||
*/ |
|||
bodyContent.appendChild(tooltipNode); |
|||
function createContentElement(originalTitle) { |
|||
} |
|||
const $contentContainer = $("<div>"); |
|||
originalTitle.split("\n").forEach(line => { |
|||
opacity: 1 |
|||
const trimmedLine = line.trim(); |
|||
if (trimmedLine) { |
|||
$contentContainer.append($("<p>").text(trimmedLine)); |
|||
} |
} |
||
}); |
|||
return $contentContainer[0]; |
|||
} |
|||
/** |
|||
// 保存原始title并清空 |
|||
* 创建弹窗DOM结构(包含内容区和箭头) |
|||
const originalTitle = this.title; |
|||
*/ |
|||
this.title = ""; |
|||
function createTooltipNode(originalTitle) { |
|||
const tooltipNode = document.createElement("ul"); |
|||
tooltipNode.className = "unihantooltip"; |
|||
// 内容区(包含提示文本和设置图标) |
|||
// 使用jQuery的on方法代替hover |
|||
const $contentLi = $("<li>"); |
|||
$(this).on(isTouchscreen ? 'click' : 'mouseenter mouseleave', function(e) { |
|||
const contentElement = createContentElement(originalTitle); |
|||
const $settingsIcon = $("<div>") |
|||
.addClass("UHsettings") |
|||
.attr("title", "设置") |
|||
.on("click", (e) => { |
|||
e.stopPropagation(); |
|||
// 示例:设置功能入口 |
|||
alert("僻字提示设置(可扩展)"); |
|||
const closeHandler = (e) => { |
|||
}); |
|||
const target = e.target || e.srcElement; |
|||
let currentElement = target; |
|||
while (currentElement && !currentElement.classList.contains("unihantooltip")) { |
|||
currentElement = currentElement.parentNode; |
|||
} |
|||
if (!currentElement) { |
|||
clearTimeout(showTimer); |
|||
hide(element); |
|||
$(bodyContent).off("click touchstart", closeHandler); |
|||
} |
|||
}; |
|||
$(bodyContent).on("click touchstart", closeHandler); |
|||
}); |
|||
} |
|||
} |
|||
// 组装内容区和箭头 |
|||
if (e.type === 'mouseleave') { |
|||
$contentLi.append(contentElement).append($settingsIcon); |
|||
clearTimeout(showTimer); |
|||
const $arrowLi = $("<li>"); |
|||
$(tooltipNode).append($contentLi).append($arrowLi); |
|||
return; |
|||
// 非触摸设备:绑定弹窗悬停事件 |
|||
if (!isTouchscreen) { |
|||
const tooltipData = { hideTimer: null, showTimer: null }; |
|||
$(tooltipNode).data("tooltipData", tooltipData) |
|||
.on("mouseenter", function() { |
|||
showTooltip($(this).data("tooltipData"), this); |
|||
}) |
|||
.on("mouseleave", function() { |
|||
hideTooltip($(this).data("tooltipData"), this); |
|||
}); |
|||
} |
|||
return tooltipNode; |
|||
} |
|||
/** |
|||
* 弹窗定位(避免超出视口,精准对齐) |
|||
*/ |
|||
function positionTooltip(element, tooltipNode) { |
|||
const $element = $(element); |
|||
const $tooltip = $(tooltipNode); |
|||
// 离线计算尺寸(避免布局抖动) |
|||
$tooltip.css({ visibility: "hidden", display: "block" }).appendTo(bodyContent); |
|||
// 元素与视口信息 |
|||
const elementOffset = $element.offset(); |
|||
const elementWidth = $element.outerWidth(); |
|||
const elementHeight = $element.outerHeight(); |
|||
const tooltipWidth = $tooltip.outerWidth(); |
|||
const contentHeight = $tooltip.find("li:first-child").outerHeight(); |
|||
const arrowHeight = 12; // 与CSS箭头高度一致 |
|||
const viewport = { |
|||
top: $(window).scrollTop(), |
|||
bottom: $(window).scrollTop() + $(window).height(), |
|||
left: 0, |
|||
right: $(window).width() |
|||
}; |
|||
// 垂直定位(优先上方,不足则翻转) |
|||
let top = elementOffset.top - contentHeight - arrowHeight; |
|||
const needFlip = top < viewport.top || (elementOffset.top + contentHeight + arrowHeight) > viewport.bottom; |
|||
$tooltip.toggleClass("UHflipped", needFlip); |
|||
if (needFlip) { |
|||
top = elementOffset.top + elementHeight + arrowHeight - 2; // 适配CSS翻转间距 |
|||
} |
|||
top = Math.max(viewport.top, Math.min(top, viewport.bottom - contentHeight)); |
|||
// 水平定位(居中对齐,留边距) |
|||
let left = elementOffset.left + (elementWidth - tooltipWidth) / 2; |
|||
left = Math.max(viewport.left + 10, Math.min(left, viewport.right - tooltipWidth - 10)); |
|||
// 箭头定位(指向元素中心) |
|||
const arrowOffset = elementOffset.left - left + elementWidth / 2 - 7; |
|||
$tooltip.find("li:last-child").css("margin-inline-start", `${arrowOffset}px`); |
|||
// 应用定位并显示 |
|||
$tooltip.css({ top: `${top}px`, left: `${left}px`, visibility: "visible", display: "" }); |
|||
} |
|||
/** |
|||
* 显示弹窗(依赖CSS过渡,无JS动画依赖) |
|||
*/ |
|||
function showTooltip(tooltipData, tooltipNode) { |
|||
const $tooltip = $(tooltipNode); |
|||
// 确保弹窗已插入DOM |
|||
if (!tooltipNode.parentNode || tooltipNode.parentNode.nodeType === 11) { |
|||
bodyContent.appendChild(tooltipNode); |
|||
} |
|||
// 添加可见类(触发CSS渲染) |
|||
$tooltip.addClass("is-visible"); |
|||
// 直接设置透明度(CSS过渡自动处理动画) |
|||
$tooltip.stop().css("opacity", 1); |
|||
// 清除隐藏定时器 |
|||
if (tooltipData.hideTimer) { |
|||
clearTimeout(tooltipData.hideTimer); |
|||
tooltipData.hideTimer = null; |
|||
} |
|||
} |
|||
/** |
|||
* 隐藏弹窗(依赖CSS过渡,无JS动画依赖) |
|||
*/ |
|||
function hideTooltip(tooltipData, tooltipNode) { |
|||
const $tooltip = $(tooltipNode); |
|||
// 清除显示定时器 |
|||
if (tooltipData.showTimer) { |
|||
clearTimeout(tooltipData.showTimer); |
|||
tooltipData.showTimer = null; |
|||
} |
|||
// 延迟隐藏(防误触) |
|||
tooltipData.hideTimer = setTimeout(() => { |
|||
// 触发淡出(CSS过渡处理) |
|||
$tooltip.stop().css("opacity", 0); |
|||
// 过渡结束后清理 |
|||
setTimeout(() => { |
|||
$tooltip.removeClass("is-visible"); |
|||
if (bodyContent.contains(tooltipNode)) { |
|||
bodyContent.removeChild(tooltipNode); |
|||
} |
} |
||
}, 150); // 与CSS transition时长一致 |
|||
}, isTouchscreen ? 16 : 100); |
|||
} |
|||
/** |
|||
showTimer && clearTimeout(showTimer); |
|||
* 触摸设备:点击外部关闭弹窗 |
|||
showTimer = setTimeout(() => { |
|||
*/ |
|||
const tooltipContent = $("<ul class='reflist'></ul>"); |
|||
function setupClickOutsideHandler(tooltipData, tooltipNode, element) { |
|||
const lines = originalTitle.split("\n"); |
|||
const handler = function(e) { |
|||
if (!tooltipNode.contains(e.target) && e.target !== element) { |
|||
hideTooltip(tooltipData, tooltipNode); |
|||
$(document).off("click touchstart", handler); // 移除事件避免泄漏 |
|||
tooltipContent.append($("<li></li>").text(line)).append("<br>"); |
|||
if (e.type === "touchstart") e.preventDefault(); // 防点击穿透 |
|||
} |
|||
}; |
|||
$(document).on("click touchstart", handler); |
|||
} |
|||
/** |
|||
if (!tooltipNode) { |
|||
* 触发弹窗显示 |
|||
tooltipNode = document.createElement("div"); |
|||
*/ |
|||
tooltipNode.className = "unihantooltip"; |
|||
function triggerTooltip(element, tooltipData) { |
|||
$(tooltipNode).append(tooltipContent); |
|||
if (!tooltipData.originalTitle.trim()) return; |
|||
if (!isTouchscreen) { |
|||
$(tooltipNode).on("mouseenter", show) |
|||
.on("mouseleave", () => hide(element)); |
|||
} |
|||
} else { |
|||
$(tooltipNode).empty().append(tooltipContent); |
|||
} |
|||
// 创建或更新弹窗 |
|||
if (!tooltipData.tooltipNode) { |
|||
tooltipData.tooltipNode = createTooltipNode(tooltipData.originalTitle); |
|||
} else { |
|||
const $contentContainer = $(tooltipData.tooltipNode).find("li:first-child > div"); |
|||
width: document.documentElement.clientWidth, |
|||
$contentContainer.replaceWith(createContentElement(tooltipData.originalTitle)); |
|||
height: document.documentElement.clientHeight |
|||
} |
|||
let top = position.top - tooltipNode.offsetHeight; |
|||
let left = position.left - 7; |
|||
// 定位并显示 |
|||
positionTooltip(element, tooltipData.tooltipNode); |
|||
if (top < 0) { |
|||
showTooltip(tooltipData, tooltipData.tooltipNode); |
|||
top = position.top + $(element).outerHeight(); |
|||
} else { |
|||
$(tooltipNode).removeClass("UHflipped"); |
|||
} |
|||
// 触摸设备绑定外部点击关闭 |
|||
if (left + tooltipNode.offsetWidth > viewport.width) { |
|||
if (isTouchscreen) { |
|||
left = viewport.width - tooltipNode.offsetWidth - 10; |
|||
setupClickOutsideHandler(tooltipData, tooltipData.tooltipNode, element); |
|||
} |
|||
} |
|||
} |
|||
/*===== 事件委托:支持动态元素 =====*/ |
|||
$(tooltipNode).css({ |
|||
if (isTouchscreen) { |
|||
top: Math.max(0, top), |
|||
// 触摸设备:点击触发 |
|||
left: Math.max(0, left), |
|||
$bodyContent.on("click", ".inline-unihan", function(e) { |
|||
opacity: 0 // 初始设置为透明以准备动画 |
|||
e.preventDefault(); |
|||
const element = this; |
|||
const tooltipData = initElementData(element); |
|||
// 切换显示/隐藏状态 |
|||
if (tooltipData.tooltipNode && bodyContent.contains(tooltipData.tooltipNode)) { |
|||
hideTooltip(tooltipData, tooltipData.tooltipNode); |
|||
} else { |
|||
triggerTooltip(element, tooltipData); |
|||
} |
|||
}); |
}); |
||
} |
} else { |
||
// 非触摸设备:鼠标悬停触发 |
|||
})(); |
|||
$bodyContent.on("mouseenter", ".inline-unihan", function() { |
|||
const element = this; |
|||
const tooltipData = initElementData(element); |
|||
tooltipData.showTimer = setTimeout(() => { |
|||
triggerTooltip(element, tooltipData); |
|||
}, hoverDelay); |
|||
}).on("mouseleave", ".inline-unihan", function() { |
|||
const element = this; |
|||
const tooltipData = initElementData(element); |
|||
if (tooltipData.tooltipNode) { |
|||
hideTooltip(tooltipData, tooltipData.tooltipNode); |
|||
} |
|||
}); |
|||
} |
|||
/*===== 窗口滚动时重新定位弹窗 =====*/ |
|||
$(window).on("scroll", function() { |
|||
$(".inline-unihan").each(function() { |
|||
const tooltipData = $(this).data("tooltipData"); |
|||
if (tooltipData && tooltipData.tooltipNode && bodyContent.contains(tooltipData.tooltipNode)) { |
|||
positionTooltip(this, tooltipData.tooltipNode); |
|||
} |
|||
}); |
|||
}); |
|||
})(jQuery, mediaWiki); |
|||
2025年11月11日 (二) 12:45的最新版本
/*
本小工具可以將[[Template:僻字]]的提示由原來的title提示改為元素式彈出提示,使觸控式裝置可以觀看有關提示
适配说明:兼容MediaWiki 1.43+,修复模块依赖错误,移除jquery.ui.animate依赖,优化动画逻辑
*/
// 显式传入jQuery和mediaWiki,避免$冲突
(function($, mw) {
"use strict";
/*===== 初始化检查:控制工具是否加载 =====*/
// 获取URL参数"UTdontload"(0=清除不加载状态,数字=设置不加载状态)
const dontLoadParam = mw.util.getParamValue("UTdontload");
if (dontLoadParam !== null) {
const paramVal = parseInt(dontLoadParam, 10);
if (!isNaN(paramVal)) {
paramVal === 0 ? mw.storage.remove("UTdontload") : mw.storage.set("UTdontload", "1");
}
}
// 若本地存储存在"不加载"状态,直接终止
if (mw.storage.get("UTdontload") === "1") {
return;
}
/*===== 主初始化:命名空间与容器 =====*/
const canonicalNamespace = mw.config.get("wgCanonicalNamespace");
const allowedNamespaces = ["", "Project", "Help"]; // 主/项目/帮助命名空间
if (!allowedNamespaces.includes(canonicalNamespace)) {
return;
}
// 获取内容容器(优先mw-content-text,兼容旧版本)
const $bodyContent = $("#mw-content-text").length ? $("#mw-content-text") : $("body");
const bodyContent = $bodyContent[0];
/*===== 设备类型检测 =====*/
const isTouchscreen = window.matchMedia("(hover: none), (pointer: coarse)").matches ||
mw.config.get("wgDisplayResolution") === "mobile" ||
"ontouchstart" in document.documentElement;
const hoverDelay = isTouchscreen ? 0 : 200; // 触摸设备无延迟
/*===== 工具函数:弹窗核心逻辑 =====*/
/**
* 初始化元素数据(存储到元素data中)
*/
function initElementData(element) {
let tooltipData = $(element).data("tooltipData");
if (!tooltipData) {
const originalTitle = element.title || "";
element.title = ""; // 清空原生title提示
$(element).addClass("UHTarget"); // 添加高亮类
tooltipData = {
originalTitle: originalTitle,
tooltipNode: null,
hideTimer: null,
showTimer: null
};
$(element).data("tooltipData", tooltipData);
}
return tooltipData;
}
/**
* 创建弹窗内容(支持多行文本分割)
*/
function createContentElement(originalTitle) {
const $contentContainer = $("<div>");
originalTitle.split("\n").forEach(line => {
const trimmedLine = line.trim();
if (trimmedLine) {
$contentContainer.append($("<p>").text(trimmedLine));
}
});
return $contentContainer[0];
}
/**
* 创建弹窗DOM结构(包含内容区和箭头)
*/
function createTooltipNode(originalTitle) {
const tooltipNode = document.createElement("ul");
tooltipNode.className = "unihantooltip";
// 内容区(包含提示文本和设置图标)
const $contentLi = $("<li>");
const contentElement = createContentElement(originalTitle);
const $settingsIcon = $("<div>")
.addClass("UHsettings")
.attr("title", "设置")
.on("click", (e) => {
e.stopPropagation();
// 示例:设置功能入口
alert("僻字提示设置(可扩展)");
});
// 组装内容区和箭头
$contentLi.append(contentElement).append($settingsIcon);
const $arrowLi = $("<li>");
$(tooltipNode).append($contentLi).append($arrowLi);
// 非触摸设备:绑定弹窗悬停事件
if (!isTouchscreen) {
const tooltipData = { hideTimer: null, showTimer: null };
$(tooltipNode).data("tooltipData", tooltipData)
.on("mouseenter", function() {
showTooltip($(this).data("tooltipData"), this);
})
.on("mouseleave", function() {
hideTooltip($(this).data("tooltipData"), this);
});
}
return tooltipNode;
}
/**
* 弹窗定位(避免超出视口,精准对齐)
*/
function positionTooltip(element, tooltipNode) {
const $element = $(element);
const $tooltip = $(tooltipNode);
// 离线计算尺寸(避免布局抖动)
$tooltip.css({ visibility: "hidden", display: "block" }).appendTo(bodyContent);
// 元素与视口信息
const elementOffset = $element.offset();
const elementWidth = $element.outerWidth();
const elementHeight = $element.outerHeight();
const tooltipWidth = $tooltip.outerWidth();
const contentHeight = $tooltip.find("li:first-child").outerHeight();
const arrowHeight = 12; // 与CSS箭头高度一致
const viewport = {
top: $(window).scrollTop(),
bottom: $(window).scrollTop() + $(window).height(),
left: 0,
right: $(window).width()
};
// 垂直定位(优先上方,不足则翻转)
let top = elementOffset.top - contentHeight - arrowHeight;
const needFlip = top < viewport.top || (elementOffset.top + contentHeight + arrowHeight) > viewport.bottom;
$tooltip.toggleClass("UHflipped", needFlip);
if (needFlip) {
top = elementOffset.top + elementHeight + arrowHeight - 2; // 适配CSS翻转间距
}
top = Math.max(viewport.top, Math.min(top, viewport.bottom - contentHeight));
// 水平定位(居中对齐,留边距)
let left = elementOffset.left + (elementWidth - tooltipWidth) / 2;
left = Math.max(viewport.left + 10, Math.min(left, viewport.right - tooltipWidth - 10));
// 箭头定位(指向元素中心)
const arrowOffset = elementOffset.left - left + elementWidth / 2 - 7;
$tooltip.find("li:last-child").css("margin-inline-start", `${arrowOffset}px`);
// 应用定位并显示
$tooltip.css({ top: `${top}px`, left: `${left}px`, visibility: "visible", display: "" });
}
/**
* 显示弹窗(依赖CSS过渡,无JS动画依赖)
*/
function showTooltip(tooltipData, tooltipNode) {
const $tooltip = $(tooltipNode);
// 确保弹窗已插入DOM
if (!tooltipNode.parentNode || tooltipNode.parentNode.nodeType === 11) {
bodyContent.appendChild(tooltipNode);
}
// 添加可见类(触发CSS渲染)
$tooltip.addClass("is-visible");
// 直接设置透明度(CSS过渡自动处理动画)
$tooltip.stop().css("opacity", 1);
// 清除隐藏定时器
if (tooltipData.hideTimer) {
clearTimeout(tooltipData.hideTimer);
tooltipData.hideTimer = null;
}
}
/**
* 隐藏弹窗(依赖CSS过渡,无JS动画依赖)
*/
function hideTooltip(tooltipData, tooltipNode) {
const $tooltip = $(tooltipNode);
// 清除显示定时器
if (tooltipData.showTimer) {
clearTimeout(tooltipData.showTimer);
tooltipData.showTimer = null;
}
// 延迟隐藏(防误触)
tooltipData.hideTimer = setTimeout(() => {
// 触发淡出(CSS过渡处理)
$tooltip.stop().css("opacity", 0);
// 过渡结束后清理
setTimeout(() => {
$tooltip.removeClass("is-visible");
if (bodyContent.contains(tooltipNode)) {
bodyContent.removeChild(tooltipNode);
}
}, 150); // 与CSS transition时长一致
}, isTouchscreen ? 16 : 100);
}
/**
* 触摸设备:点击外部关闭弹窗
*/
function setupClickOutsideHandler(tooltipData, tooltipNode, element) {
const handler = function(e) {
if (!tooltipNode.contains(e.target) && e.target !== element) {
hideTooltip(tooltipData, tooltipNode);
$(document).off("click touchstart", handler); // 移除事件避免泄漏
if (e.type === "touchstart") e.preventDefault(); // 防点击穿透
}
};
$(document).on("click touchstart", handler);
}
/**
* 触发弹窗显示
*/
function triggerTooltip(element, tooltipData) {
if (!tooltipData.originalTitle.trim()) return;
// 创建或更新弹窗
if (!tooltipData.tooltipNode) {
tooltipData.tooltipNode = createTooltipNode(tooltipData.originalTitle);
} else {
const $contentContainer = $(tooltipData.tooltipNode).find("li:first-child > div");
$contentContainer.replaceWith(createContentElement(tooltipData.originalTitle));
}
// 定位并显示
positionTooltip(element, tooltipData.tooltipNode);
showTooltip(tooltipData, tooltipData.tooltipNode);
// 触摸设备绑定外部点击关闭
if (isTouchscreen) {
setupClickOutsideHandler(tooltipData, tooltipData.tooltipNode, element);
}
}
/*===== 事件委托:支持动态元素 =====*/
if (isTouchscreen) {
// 触摸设备:点击触发
$bodyContent.on("click", ".inline-unihan", function(e) {
e.preventDefault();
const element = this;
const tooltipData = initElementData(element);
// 切换显示/隐藏状态
if (tooltipData.tooltipNode && bodyContent.contains(tooltipData.tooltipNode)) {
hideTooltip(tooltipData, tooltipData.tooltipNode);
} else {
triggerTooltip(element, tooltipData);
}
});
} else {
// 非触摸设备:鼠标悬停触发
$bodyContent.on("mouseenter", ".inline-unihan", function() {
const element = this;
const tooltipData = initElementData(element);
tooltipData.showTimer = setTimeout(() => {
triggerTooltip(element, tooltipData);
}, hoverDelay);
}).on("mouseleave", ".inline-unihan", function() {
const element = this;
const tooltipData = initElementData(element);
if (tooltipData.tooltipNode) {
hideTooltip(tooltipData, tooltipData.tooltipNode);
}
});
}
/*===== 窗口滚动时重新定位弹窗 =====*/
$(window).on("scroll", function() {
$(".inline-unihan").each(function() {
const tooltipData = $(this).data("tooltipData");
if (tooltipData && tooltipData.tooltipNode && bodyContent.contains(tooltipData.tooltipNode)) {
positionTooltip(this, tooltipData.tooltipNode);
}
});
});
})(jQuery, mediaWiki);