Laycookie
Laycookie

Reputation: 735

Svelte can't find .ScrollTop of a variable

Hello I have an element which is being referenced to the script like this

<div bind:this={AboutMeAnimation} />

and then In my script I console log ScrollTop value

    let AboutMeAnimation;
    console.log(AboutMeAnimation.ScrollTop);

and for some resson I'm getting an error can some one tell me why?enter image description here

Upvotes: 0

Views: 421

Answers (1)

Alexander Staroselsky
Alexander Staroselsky

Reputation: 38807

A couple of issues, the first is that is scrollTop with a lowercase “s”, the other is that the element does not bind immediately so if you are putting this statement at the top level of the component it will be undefined for some period of time. You can use onMount or similar to get this value:

onMount(() => {
  console.log(AboutMeAnimation.scrollTop);
});

Otherwise a reactive statement could work also:

$: {
  console.log(AboutMeAnimation && AboutMeAnimation.scrollTop);
}

Hopefully that helps!

Upvotes: 2

Related Questions