MediaWiki:Common.js: Difference between revisions
MediaWiki interface page
More actions
Jonatanktk (talk | contribs) Created page with "→Any JavaScript here will be loaded for all users on every page load.: document.addEventListener("DOMContentLoaded", () => { const tooltip = document.createElement("div"); tooltip.className = "custom-tooltip"; document.body.appendChild(tooltip); let activeElement = null; let storedTitle = null; document.addEventListener("mouseover", e => { const target = e.target.closest("[title], [alt]"); if (!target) return; // Tooltip-Text: bevorzugt alt..." |
Jonatanktk (talk | contribs) No edit summary |
||
| Line 1: | Line 1: | ||
/* Any JavaScript here will be loaded for all users on every page load. */ | /* Any JavaScript here will be loaded for all users on every page load. */ | ||
(function () { | |||
const tooltip = document.createElement("div"); | const tooltip = document.createElement("div"); | ||
tooltip.className = "custom-tooltip"; | tooltip.className = "custom-tooltip"; | ||
document.body.appendChild(tooltip); | document.body.appendChild(tooltip); | ||
let | let active = null; | ||
let storedTitle = null; | let storedTitle = null; | ||
function showTooltip(target, x, y) { | |||
const text = target.getAttribute("alt") || target.getAttribute("title"); | const text = target.getAttribute("alt") || target.getAttribute("title"); | ||
if (!text) return; | if (!text) return; | ||
// | // Browser-Tooltip deaktivieren | ||
if (target.hasAttribute("title")) { | if (target.hasAttribute("title")) { | ||
storedTitle = target.getAttribute("title"); | storedTitle = target.getAttribute("title"); | ||
| Line 22: | Line 18: | ||
} | } | ||
tooltip.textContent = text; | tooltip.textContent = text; | ||
tooltip.style.display = "block"; | tooltip.style.display = "block"; | ||
const updatePosition = ( | const updatePosition = (cx, cy) => { | ||
const | const rect = tooltip.getBoundingClientRect(); | ||
const offset = 12; | const offset = 12; | ||
let left = cx + offset; | |||
let left = | let top = cy + offset; | ||
let top = | if (left + rect.width > window.innerWidth - 10) | ||
left = cx - rect.width - offset; | |||
if (left + | if (top + rect.height > window.innerHeight - 10) | ||
left = | top = cy - rect.height - offset; | ||
if (top + | |||
top = | |||
tooltip.style.left = `${left}px`; | tooltip.style.left = `${left}px`; | ||
tooltip.style.top = `${top}px`; | tooltip.style.top = `${top}px`; | ||
| Line 45: | Line 35: | ||
const moveHandler = e => updatePosition(e.clientX, e.clientY); | const moveHandler = e => updatePosition(e.clientX, e.clientY); | ||
updatePosition( | updatePosition(x, y); | ||
document.addEventListener("mousemove", moveHandler); | document.addEventListener("mousemove", moveHandler); | ||
target.addEventListener("mouseleave", () => { | target.addEventListener( | ||
"mouseleave", | |||
() => { | |||
tooltip.classList.remove("visible"); | |||
tooltip.style.display = "none"; | |||
document.removeEventListener("mousemove", moveHandler); | |||
if (storedTitle) { | |||
target.setAttribute("title", storedTitle); | |||
storedTitle = null; | |||
} | |||
active = null; | |||
}, | |||
{ once: true } | |||
); | |||
requestAnimationFrame(() => tooltip.classList.add("visible")); | requestAnimationFrame(() => tooltip.classList.add("visible")); | ||
} | |||
// Globaler Mouseover – auch für nachgeladene Elemente | |||
document.addEventListener("mouseover", e => { | |||
const target = e.target.closest("[title], [alt]"); | |||
if (!target || active === target) return; | |||
active = target; | |||
const x = e.clientX; | |||
const y = e.clientY; | |||
showTooltip(target, x, y); | |||
}); | |||
// optional: bei DOM-Änderungen Tooltips neu aktivieren (z. B. wenn Skin-Module UI nachladen) | |||
const observer = new MutationObserver(() => { | |||
// nichts zu tun – der Eventlistener ist global, aber dies stellt sicher, dass der Tooltip-Container immer oben bleibt | |||
document.body.appendChild(tooltip); | |||
}); | }); | ||
}); | observer.observe(document.body, { childList: true, subtree: true }); | ||
})(); | |||
Revision as of 23:30, 1 November 2025
/* Any JavaScript here will be loaded for all users on every page load. */
(function () {
const tooltip = document.createElement("div");
tooltip.className = "custom-tooltip";
document.body.appendChild(tooltip);
let active = null;
let storedTitle = null;
function showTooltip(target, x, y) {
const text = target.getAttribute("alt") || target.getAttribute("title");
if (!text) return;
// Browser-Tooltip deaktivieren
if (target.hasAttribute("title")) {
storedTitle = target.getAttribute("title");
target.removeAttribute("title");
}
tooltip.textContent = text;
tooltip.style.display = "block";
const updatePosition = (cx, cy) => {
const rect = tooltip.getBoundingClientRect();
const offset = 12;
let left = cx + offset;
let top = cy + offset;
if (left + rect.width > window.innerWidth - 10)
left = cx - rect.width - offset;
if (top + rect.height > window.innerHeight - 10)
top = cy - rect.height - offset;
tooltip.style.left = `${left}px`;
tooltip.style.top = `${top}px`;
};
const moveHandler = e => updatePosition(e.clientX, e.clientY);
updatePosition(x, y);
document.addEventListener("mousemove", moveHandler);
target.addEventListener(
"mouseleave",
() => {
tooltip.classList.remove("visible");
tooltip.style.display = "none";
document.removeEventListener("mousemove", moveHandler);
if (storedTitle) {
target.setAttribute("title", storedTitle);
storedTitle = null;
}
active = null;
},
{ once: true }
);
requestAnimationFrame(() => tooltip.classList.add("visible"));
}
// Globaler Mouseover – auch für nachgeladene Elemente
document.addEventListener("mouseover", e => {
const target = e.target.closest("[title], [alt]");
if (!target || active === target) return;
active = target;
const x = e.clientX;
const y = e.clientY;
showTooltip(target, x, y);
});
// optional: bei DOM-Änderungen Tooltips neu aktivieren (z. B. wenn Skin-Module UI nachladen)
const observer = new MutationObserver(() => {
// nichts zu tun – der Eventlistener ist global, aber dies stellt sicher, dass der Tooltip-Container immer oben bleibt
document.body.appendChild(tooltip);
});
observer.observe(document.body, { childList: true, subtree: true });
})();