scrollIntoView with false aligns an element to the bottom of its scroll container
February 1, 2025
Pass false to Element.scrollIntoView()
and the element ends up at the bottom of the scrollable area. (The method scrolls the element’s parent container until
the element is visible.) That’s perfect for keeping the selected item in view while you move through a list with the
arrow keys.
I used it in the search on this blog. When you press an arrow key, Phoenix LiveView pushes an event to the client, and the client scrolls the active result into view:
window.addEventListener("phx:search-choose", (e) => {
const activeItem = this.el.querySelector(".menu-active")
activeItem.scrollIntoView(false)
})
The argument controls the alignment:
true(default): aligns to the top of the scrollable areafalse: aligns to the bottom- An object for more control:
{ behavior: "smooth", block: "nearest" }