Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting scroll of element in svelte

Tags:

svelte

I have a div with style overflow: scroll and it is overflowing on the X-axis. I would like to get the value of how far left/right the user has scrolled within this element, but I can't really figure it out.

I saw that you can quite easily bind the window scroll, but as it is just an element that is scrolling and not the window this won't work. https://svelte.dev/tutorial/svelte-window-bindings

so I tried binding the element, but couldn't really get any meaningful data out of it.

<script>
  let content;
</script>

<div class="carousel">
  <div class="content" bind:this={content}>
    ...
  </div>
</div>
{#if content}
{content.scrollLeft}
{/if}

is it at all possible and am I just missing something?

like image 763
munHunger Avatar asked Oct 15 '25 16:10

munHunger


1 Answers

You have to find the scroll left of the container/carousel. Not for content. below code will work. check this repl

<script>
  let carousel, sleft;
</script>

<div class="carousel" bind:this={carousel} 
on:scroll={()=>sleft=carousel.scrollLeft}>
  <div class="content" >
    ...
  </div>
</div>

{#if carousel}
{sleft}
{/if}
like image 190
dagalti Avatar answered Oct 18 '25 08:10

dagalti



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!