I'm struggling to achieve an infinite x-scroll using HTMX. The issue I'm facing is that the request keeps getting triggered endlessly because the element that triggers it gets inserted on the left side of the div, causing a new request to load items inside the div.
Is there a way to prevent this from happening? Perhaps by appending the new elements while maintaining the x scroll position? It seems like it could be related to either a CSS or an HTMX problem, but I'm not certain.
<div class="flex h-full space-x-2">
<div class="bg-amber-600 flex-initial w-1/4"></div>
<div class="flex space-x-2 w-full overflow-x-auto">
<div class="flex-none w-16 ml-auto"
hx-get="/scroll"
hx-trigger="intersect once"
hx-swap="outerHTML">
</div>
<div class="bg-amber-400 flex-none w-16">0</div>
<div class="bg-amber-400 flex-none w-16">1</div>
<div class="bg-amber-400 flex-none w-16">2</div>
<div class="bg-amber-400 flex-none w-16">3</div>
</div>
</div>
I also attempted using beforebegin
, but encountered the same behavior:
<div class="bg-amber-400 flex-none w-16 ml-auto"
hx-get="/scroll"
hx-trigger="intersect once"
hx-swap="beforebegin">x</div>
To better illustrate my issue, here's a codepen link: https://codepen.io/viggiesmalls/pen/RwQPROJ