Jump to content

Template:Tt/sandbox: Difference between revisions

From Detective Conan Wiki
No edit summary
Code update
Line 168: Line 168:


<pre>
<pre>
    document.addEventListener("DOMContentLoaded", function () {
     var tooltips = document.querySelectorAll(".ttt-container");
     var tooltips = document.querySelectorAll(".ttt-container");
 
   
     function adjustTooltipPositions() {
     function adjustTooltipPositions() {
         Array.prototype.forEach.call(tooltips, function (tooltipContainer) {
         Array.prototype.forEach.call(tooltips, function (tooltipContainer) {
             var tooltip = tooltipContainer.querySelector(".ttt-tooltip");
             var tooltip = tooltipContainer.querySelector(".ttt-tooltip");
             var chevron = tooltip.querySelector(".ttt-chevron");
             var chevron = tooltip.querySelector(".ttt-chevron");
 
           
             // 1. Temporarily show tooltip to measure size
             // 1. Temporarily show tooltip to measure size
             tooltipContainer.style.opacity = "0";
             tooltipContainer.style.opacity = "0";
             tooltipContainer.style.display = "flex";
             tooltipContainer.style.display = "flex";
             tooltip.style.transform = "";
             tooltip.style.transform = "";
 
           
             var tooltipRect = tooltip.getBoundingClientRect();
             var tooltipRect = tooltip.getBoundingClientRect();
             var targetRect = tooltipContainer.parentElement.getBoundingClientRect();
             var targetRect = tooltipContainer.parentElement.getBoundingClientRect();
 
           
             // 2. Measure overflow
             // 2. Measure how much the tooltip is outside the viewport
             var overflowTop = tooltipRect.top < 1;
             var overflowTop = tooltipRect.top < 1;
             var spaceTop = targetRect.top > tooltipRect.height + 4;
             var spaceTop = targetRect.top > tooltipRect.height + 4;
             var overflowLeft = Math.min(Math.max(0, -tooltipRect.left), 0.5 * tooltipRect.width - 15);
             var overflowLeft = Math.min(Math.max(0, -tooltipRect.left), 0.5 * tooltipRect.width - 15);
             var overflowRight = Math.min(Math.max(0, tooltipRect.right - document.documentElement.clientWidth), 0.5 * tooltipRect.width - 15);
             var overflowRight = Math.min(Math.max(0, tooltipRect.right - document.documentElement.clientWidth), 0.5 * tooltipRect.width - 15);
 
           
             // 3. Reposition top/bottom
             // 3. Shift tooltip into view if needed and move chevron accordingly
             if (overflowTop) {
             if (overflowTop) {
                 tooltipContainer.classList.remove("ttt-container-above");
                 tooltipContainer.classList.remove("ttt-container-above");
Line 198: Line 198:
                 tooltipContainer.classList.remove("ttt-container-below");
                 tooltipContainer.classList.remove("ttt-container-below");
             }
             }
 
              
             // 4. Reposition left/right
             if (overflowLeft > 0) {
             if (overflowLeft > 0) {
                 tooltip.style.transform = "translateX(calc(" + overflowLeft + "px + 4px))";
                 tooltip.style.transform = "translateX(calc(" + overflowLeft + "px + 4px))";
Line 210: Line 209:
                 chevron.style.transform = "";
                 chevron.style.transform = "";
             }
             }
 
           
             // 5. Restore styles
             // 4. Hide it again until hover
             tooltipContainer.style.opacity = "";
             tooltipContainer.style.opacity = "";
             tooltipContainer.style.display = "";
             tooltipContainer.style.display = "";
         });
         });
     }
     }
 
   
     // Run on page load, resize, scroll
     // Run on page load, resize, and scroll
     adjustTooltipPositions();
     adjustTooltipPositions();
     window.addEventListener("resize", adjustTooltipPositions);
     window.addEventListener("resize", adjustTooltipPositions);
     window.addEventListener("scroll", adjustTooltipPositions, true);
     window.addEventListener("scroll", adjustTooltipPositions, true);
 
});
 
// ADDITIONAL CODE FROM CHATGPT THAT FIXES ISSUE ON MOBILE (i think)
    // Also run when tooltip is triggered via interaction
    function onShow(e) {
        var target = e.target.closest ? e.target.closest(".ttt-target") : null;
        if (!target) return;
 
        var tooltipContainer = target.querySelector(".ttt-container");
        if (!tooltipContainer) return;
 
        tooltipContainer.style.display = "flex";
        adjustTooltipPositions();
    }
 
    document.addEventListener("touchstart", onShow);
</pre>
</pre>
</noinclude>
</noinclude>

Revision as of 21:52, 10 June 2025

We love DC[2][3]
































































    document.addEventListener("DOMContentLoaded", function () {
    var tooltips = document.querySelectorAll(".ttt-container");
    
    function adjustTooltipPositions() {
        Array.prototype.forEach.call(tooltips, function (tooltipContainer) {
            var tooltip = tooltipContainer.querySelector(".ttt-tooltip");
            var chevron = tooltip.querySelector(".ttt-chevron");
            
            // 1. Temporarily show tooltip to measure size
            tooltipContainer.style.opacity = "0";
            tooltipContainer.style.display = "flex";
            tooltip.style.transform = "";
            
            var tooltipRect = tooltip.getBoundingClientRect();
            var targetRect = tooltipContainer.parentElement.getBoundingClientRect();
            
            // 2. Measure how much the tooltip is outside the viewport
            var overflowTop = tooltipRect.top < 1;
            var spaceTop = targetRect.top > tooltipRect.height + 4;
            var overflowLeft = Math.min(Math.max(0, -tooltipRect.left), 0.5 * tooltipRect.width - 15);
            var overflowRight = Math.min(Math.max(0, tooltipRect.right - document.documentElement.clientWidth), 0.5 * tooltipRect.width - 15);
            
            // 3. Shift tooltip into view if needed and move chevron accordingly
            if (overflowTop) {
                tooltipContainer.classList.remove("ttt-container-above");
                tooltipContainer.classList.add("ttt-container-below");
            } else if (spaceTop) {
                tooltipContainer.classList.add("ttt-container-above");
                tooltipContainer.classList.remove("ttt-container-below");
            }
            
            if (overflowLeft > 0) {
                tooltip.style.transform = "translateX(calc(" + overflowLeft + "px + 4px))";
                chevron.style.transform = "translateX(calc(-50% - " + overflowLeft + "px - 4px))";
            } else if (overflowRight > 0) {
                tooltip.style.transform = "translateX(calc(-" + overflowRight + "px - 4px))";
                chevron.style.transform = "translateX(calc(-50% + " + overflowRight + "px + 4px))";
            } else {
                tooltip.style.transform = "";
                chevron.style.transform = "";
            }
            
            // 4. Hide it again until hover
            tooltipContainer.style.opacity = "";
            tooltipContainer.style.display = "";
        });
    }
    
    // Run on page load, resize, and scroll
    adjustTooltipPositions();
    window.addEventListener("resize", adjustTooltipPositions);
    window.addEventListener("scroll", adjustTooltipPositions, true);
});
  1. ^ wjdid
  2. ^ qwoidjqwoi
  3. ^ e9qjr