svelte-scroller icon indicating copy to clipboard operation
svelte-scroller copied to clipboard

Execute functions based on scroll index

Open jbixon13 opened this issue 5 years ago • 3 comments

Is it possible to execute arbitrary functions such as:

<script>
	let index 

	function count() {
		console.log('this executes upon scrolling to the third section');
	}

	if (index = 2) {
		count()
	}
</script>

This is possible in something like scrollama.js, and I would like to use svelte-scroller to do so if possible as it's already built into Svelte.

jbixon13 avatar Sep 14 '20 17:09 jbixon13

I think it should work if you do this:

$: if (index == 2) {
    count();
}

I've opened PR #14 to avoid repeated calls to the function (you could alternatively use another variable to keep track of whether index has changed from its previous value).

jtrim-ons avatar Mar 29 '21 10:03 jtrim-ons

Hello! I was wondering this too. I tried this as well but my function ended up getting called over 100 times and glitching.

maxgraze-zz avatar Apr 16 '21 21:04 maxgraze-zz

@maxgraze here is the version where I tried to fix the issue of repeated function calls: https://github.com/jtrim-ons/svelte-scroller/blob/fewer-section-updates/Scroller.svelte . If you decide to give it a try, let us know if it works.

jtrim-ons avatar Apr 17 '21 14:04 jtrim-ons