Window Scroll
Reactive window scroll.
๐ฌ Usage
๐ฉโ๐ปAPI
โฉ๏ธ Returns
Name | Description | Type |
---|---|---|
x | A readable store with the x position | Readable<number> |
y | A readable store with the y position | Readable<number> |
๐งช Playground
Source Code ๐
Source Code
import { browser, to_readable, to_writable } from "@sveu/shared"
import { on } from "../event_listener"
/**
* Reactive window scroll.
*
* @returns
* - `x`: A readable store with the current window scroll x position.
* - `y`: A readable store with the current window scroll y position.
*/
export function window_scroll() {
if (!browser) return { x: to_readable(0), y: to_readable(0) }
const x = to_writable(window.scrollX)
const y = to_writable(window.scrollY)
on(
window,
"scroll",
() => {
x.set(window.scrollX)
y.set(window.scrollY)
},
{
capture: false,
passive: true,
}
)
return { x: to_readable(x), y: to_readable(y) }
}
Last update: 2023-03-16
Authors: Mohamed-Kaizen