Infinite Scroll

Demonstrates infinite scrolling of a page

Back | Explain | Code | Htmx Docs
The infinite scroll pattern provides a way to load content dynamically on user scrolling action. Let’s focus on the final row (or the last element of your content): ``` def make_last_row(page, limit): return Tr(hx_trigger="revealed", hx_swap="afterend", hx_get=load_contacts.rt(page=page + 1))( Td("Smith"), Td(page * limit), ) @app.get def load_contacts(page: int, limit: int = 5): rows = [Tr(Td("Smith"), Td((page - 1) * limit + i)) for i in range(1, limit)] return *rows, make_last_row(page, limit) ``` This last element contains a listener which, when scrolled into view, will trigger a request. The result is then appended after it. The last element of the results will itself contain the listener to load the next page of results, and so on. <blockquote><ins>revealed</ins> triggered when an element is scrolled into the viewport (also useful for lazy-loading). If you are using overflow in css like overflow-y: scroll you should use intersect once instead of revealed.</blockquote>

Direct url