feat: 移除移动端的站点卡片悬停提示功能
This commit is contained in:
157
src/script.js
157
src/script.js
@@ -2138,72 +2138,121 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Tooltip functionality for truncated text
|
// Tooltip functionality for truncated text(仅桌面端/支持悬停设备启用)
|
||||||
document.addEventListener('DOMContentLoaded', () => {
|
document.addEventListener('DOMContentLoaded', () => {
|
||||||
// Create tooltip element
|
const hoverMedia =
|
||||||
const tooltip = document.createElement('div');
|
window.matchMedia && window.matchMedia('(hover: hover) and (pointer: fine)');
|
||||||
tooltip.className = 'custom-tooltip';
|
|
||||||
document.body.appendChild(tooltip);
|
|
||||||
|
|
||||||
let activeElement = null;
|
if (!hoverMedia) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Show tooltip on hover
|
let cleanupTooltip = null;
|
||||||
document.addEventListener('mouseover', (e) => {
|
|
||||||
const target = e.target.closest('[data-tooltip]');
|
function enableTooltip() {
|
||||||
if (target) {
|
if (cleanupTooltip) return;
|
||||||
const tooltipText = target.getAttribute('data-tooltip');
|
|
||||||
if (tooltipText) {
|
// Create tooltip element
|
||||||
activeElement = target;
|
const tooltip = document.createElement('div');
|
||||||
tooltip.textContent = tooltipText;
|
tooltip.className = 'custom-tooltip';
|
||||||
tooltip.classList.add('visible');
|
document.body.appendChild(tooltip);
|
||||||
updateTooltipPosition(e);
|
|
||||||
|
let activeElement = null;
|
||||||
|
|
||||||
|
function updateTooltipPosition(e) {
|
||||||
|
// Position tooltip 15px below/right of cursor
|
||||||
|
const x = e.clientX + 15;
|
||||||
|
const y = e.clientY + 15;
|
||||||
|
|
||||||
|
// Boundary checks to keep inside viewport
|
||||||
|
const rect = tooltip.getBoundingClientRect();
|
||||||
|
const winWidth = window.innerWidth;
|
||||||
|
const winHeight = window.innerHeight;
|
||||||
|
|
||||||
|
let finalX = x;
|
||||||
|
let finalY = y;
|
||||||
|
|
||||||
|
// If tooltip goes off right edge
|
||||||
|
if (x + rect.width > winWidth) {
|
||||||
|
finalX = e.clientX - rect.width - 10;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Move tooltip with cursor
|
// If tooltip goes off bottom edge
|
||||||
document.addEventListener('mousemove', (e) => {
|
if (y + rect.height > winHeight) {
|
||||||
if (activeElement) {
|
finalY = e.clientY - rect.height - 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
tooltip.style.left = finalX + 'px';
|
||||||
|
tooltip.style.top = finalY + 'px';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Show tooltip on hover
|
||||||
|
function onMouseOver(e) {
|
||||||
|
const target = e.target.closest('[data-tooltip]');
|
||||||
|
if (!target) return;
|
||||||
|
|
||||||
|
const tooltipText = target.getAttribute('data-tooltip');
|
||||||
|
if (!tooltipText) return;
|
||||||
|
|
||||||
|
activeElement = target;
|
||||||
|
tooltip.textContent = tooltipText;
|
||||||
|
tooltip.classList.add('visible');
|
||||||
updateTooltipPosition(e);
|
updateTooltipPosition(e);
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
|
||||||
// Hide tooltip on mouse out
|
// Move tooltip with cursor
|
||||||
document.addEventListener('mouseout', (e) => {
|
function onMouseMove(e) {
|
||||||
const target = e.target.closest('[data-tooltip]');
|
if (!activeElement) return;
|
||||||
if (target && target === activeElement) {
|
updateTooltipPosition(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hide tooltip on mouse out
|
||||||
|
function onMouseOut(e) {
|
||||||
|
const target = e.target.closest('[data-tooltip]');
|
||||||
|
if (!target || target !== activeElement) return;
|
||||||
|
|
||||||
// Check if we really left the element (not just went to a child)
|
// Check if we really left the element (not just went to a child)
|
||||||
if (!target.contains(e.relatedTarget)) {
|
if (target.contains(e.relatedTarget)) return;
|
||||||
activeElement = null;
|
|
||||||
tooltip.classList.remove('visible');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
function updateTooltipPosition(e) {
|
activeElement = null;
|
||||||
// Position tooltip 15px below/right of cursor
|
tooltip.classList.remove('visible');
|
||||||
const x = e.clientX + 15;
|
|
||||||
const y = e.clientY + 15;
|
|
||||||
|
|
||||||
// Boundary checks to keep inside viewport
|
|
||||||
const rect = tooltip.getBoundingClientRect();
|
|
||||||
const winWidth = window.innerWidth;
|
|
||||||
const winHeight = window.innerHeight;
|
|
||||||
|
|
||||||
let finalX = x;
|
|
||||||
let finalY = y;
|
|
||||||
|
|
||||||
// If tooltip goes off right edge
|
|
||||||
if (x + rect.width > winWidth) {
|
|
||||||
finalX = e.clientX - rect.width - 10;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If tooltip goes off bottom edge
|
document.addEventListener('mouseover', onMouseOver);
|
||||||
if (y + rect.height > winHeight) {
|
document.addEventListener('mousemove', onMouseMove);
|
||||||
finalY = e.clientY - rect.height - 10;
|
document.addEventListener('mouseout', onMouseOut);
|
||||||
}
|
|
||||||
|
|
||||||
tooltip.style.left = finalX + 'px';
|
cleanupTooltip = () => {
|
||||||
tooltip.style.top = finalY + 'px';
|
document.removeEventListener('mouseover', onMouseOver);
|
||||||
|
document.removeEventListener('mousemove', onMouseMove);
|
||||||
|
document.removeEventListener('mouseout', onMouseOut);
|
||||||
|
|
||||||
|
activeElement = null;
|
||||||
|
tooltip.classList.remove('visible');
|
||||||
|
tooltip.remove();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
function disableTooltip() {
|
||||||
|
if (!cleanupTooltip) return;
|
||||||
|
cleanupTooltip();
|
||||||
|
cleanupTooltip = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncTooltipEnabled() {
|
||||||
|
if (hoverMedia.matches) {
|
||||||
|
enableTooltip();
|
||||||
|
} else {
|
||||||
|
disableTooltip();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
syncTooltipEnabled();
|
||||||
|
|
||||||
|
// 兼容旧版 Safari:addListener/removeListener
|
||||||
|
if (hoverMedia.addEventListener) {
|
||||||
|
hoverMedia.addEventListener('change', syncTooltipEnabled);
|
||||||
|
} else if (hoverMedia.addListener) {
|
||||||
|
hoverMedia.addListener(syncTooltipEnabled);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user