document.addEventListener('DOMContentLoaded', () => { window.updateTooltips = () => { console.debug('Update tooltips'); const elements = document.querySelectorAll('.tip, .dropdown'); // Calculate max potential displacement let max = 0; elements.forEach(el => { const box = el.getBoundingClientRect(); if (max < box.height) max = box.height; }); // Prevent displacement elements.forEach(el => { if (!el.tooltipSetup) { el.tooltipSetup = true; const box = el.getBoundingClientRect(); if (box.bottom >= document.body.clientHeight - (max + 32)) { el.classList.add('top'); } } }); }; window.addEventListener('popstate', () => { updateTooltips(); }); window.requestAnimationFrame(() => { updateTooltips(); }); });