如何在Svelte项目中检查窗口的滚动位置?

huangapple go评论73阅读模式
英文:

how to check window scrollTop on svelte project?

问题

I'm working on a svelte project and I'm having issues getting the window scrollTop value when I scroll the website...

I tried running this code in the svelte page route:

<script>
	function checkScroll() {
		console.log("Scrolled");
	}
</script>
<svelte:window on:scrollX={checkScroll}/>

When I run this on dev server console remains empty...
I can't figure out what I'm doing wrong or not doing...

英文:

I'm working on a svelte project and I'm having issues getting the window scrollTop value when I scroll the website...

I tried running this code in the svelte page route:

&lt;script&gt;
	function checkScroll() {
		console.log(&quot;Scrolled&quot;);
	}
&lt;/script&gt;
&lt;svelte:window on:scrollX={checkScroll}/&gt;

When I run this on dev server console remains empty..
I can't figure out what I'm doing wrong or not doing...

答案1

得分: 1

You could bind the value directly to a variable tutorial

<script>
	let x //to the right
    let y //down
</script>

<svelte:window bind:scrollX={x} bind:scrollY={y}/>

或者根据您的需求以这种方式监听事件

<script>
	function handleScroll(event) {
		console.log('scrolling')
	}
</script>

<svelte:window on:scroll={handleScroll}/>
英文:

You could bind the value directly to a variable tutorial

&lt;script&gt;
	let x //to the right
    let y //down
&lt;/script&gt;

&lt;svelte:window bind:scrollX={x} bind:scrollY={y}/&gt;

or listen to the event like this depending on your needs

&lt;script&gt;
	function handleScroll(event) {
		console.log(&#39;scrolling&#39;)
	}
&lt;/script&gt;

&lt;svelte:window on:scroll={handleScroll}/&gt;

huangapple
  • 本文由 发表于 2023年2月24日 15:35:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定