Performance: Dropdown menu alignment
MEDIUM IMPACT
This affects how quickly and smoothly dropdown menus appear and align on the page, impacting user interaction speed and visual stability.
<div class="dropdown"> <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown" aria-expanded="false"> Dropdown button </button> <ul class="dropdown-menu dropdown-menu-end"> <li><a class="dropdown-item" href="#">Action</a></li> <li><a class="dropdown-item" href="#">Another action</a></li> </ul> </div>
document.querySelectorAll('.dropdown-toggle').forEach(btn => { btn.addEventListener('click', () => { const menu = btn.nextElementSibling; menu.style.left = (btn.getBoundingClientRect().left + window.scrollX) + 'px'; menu.style.top = (btn.getBoundingClientRect().bottom + window.scrollY) + 'px'; }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual style setting on click | Multiple style writes | 2 reflows per open | High paint cost due to layout thrashing | [X] Bad |
| Bootstrap dropdown-menu-end class | Minimal DOM changes | 1 reflow triggered by Popper.js | Low paint cost with GPU compositing | [OK] Good |