der_robert

Untitled

Jul 21st, 2025
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function showTooltip(element, islandName, username) {
  2.     try {
  3.         const tooltip = document.getElementById('tooltip');
  4.         const mapContainer = document.querySelector('.map-container');
  5.  
  6.         const containerRect = mapContainer.getBoundingClientRect();
  7.         const pointLeft = element.offsetLeft;
  8.         const pointTop = element.offsetTop;
  9.  
  10.         // Tooltip-Inhalt
  11.         tooltip.textContent = `${islandName} (${username})`;
  12.         tooltip.style.display = 'block';
  13.         tooltip.style.position = 'absolute';
  14.  
  15.         // Tooltip vorübergehend sichtbar machen für Messung
  16.         tooltip.style.visibility = 'hidden';
  17.         tooltip.style.left = '0px';
  18.         tooltip.style.top = '0px';
  19.  
  20.         // Größe berechnen
  21.         const tooltipWidth = tooltip.offsetWidth;
  22.         const tooltipHeight = tooltip.offsetHeight;
  23.  
  24.         // Zielposition (oberhalb des Punkts, zentriert)
  25.         let tooltipLeft = pointLeft + element.offsetWidth / 2 - tooltipWidth / 2;
  26.         let tooltipTop = pointTop - tooltipHeight - 10;
  27.  
  28.         // Randkorrekturen (linker und rechter Rand)
  29.         const padding = 5;
  30.         if (tooltipLeft < padding) {
  31.             tooltipLeft = padding;
  32.         } else if (tooltipLeft + tooltipWidth > mapContainer.offsetWidth - padding) {
  33.             tooltipLeft = mapContainer.offsetWidth - tooltipWidth - padding;
  34.         }
  35.  
  36.         // Falls Tooltip oberhalb des Containers rausragen würde → darunter anzeigen
  37.         if (tooltipTop < padding) {
  38.             tooltipTop = pointTop + element.offsetHeight + 10;
  39.         }
  40.  
  41.         // Endgültig setzen
  42.         tooltip.style.left = `${tooltipLeft}px`;
  43.         tooltip.style.top = `${tooltipTop}px`;
  44.         tooltip.style.visibility = 'visible';
  45.  
  46.         console.log(`Tooltip für ${username} bei x=${tooltipLeft}px, y=${tooltipTop}px`);
  47.     } catch (e) {
  48.         console.error('Error in showTooltip:', e);
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment