fn
useParallax
v0.0.14testeddemoReactive parallax effect. Prefers the device orientation sensors and
transparently falls back to mouse position when orientation is unavailable. Composes
useDeviceOrientation and useMouse; pass a target to make the mouse
fallback relative to an element's centre instead of the whole viewport. SSR-safe.
Examples
ts
const { roll, tilt, source } = useParallax(el);
// <div :style="{ transform: `rotateX(${roll * 20}deg) rotateY(${tilt * 20}deg)` }" />ts
// Viewport-relative, with custom sensitivity on the mouse fallback
const { roll, tilt } = useParallax(null, { mouseTiltAdjust: i => i * 2 });Demo
Loading demo…
Signature
ts
export function useParallax(
target?: MaybeComputedElementRef | null,
options: UseParallaxOptions ={ ... }Parameters
| Parameter | Type | Description |
|---|---|---|
target? | MaybeComputedElementRef | null | Element the mouse fallback is measured against. Omit to use the viewport. |
options? | UseParallaxOptions | Options |
Returns
UseParallaxReturnReactive roll, tilt (both -0.5 ~ 0.5), and source| Property | Type | Description |
|---|---|---|
roll | ComputedRef<number> | Roll, scaled to -0.5 ~ 0.5 (rotation around the horizontal axis). |
tilt | ComputedRef<number> | Tilt, scaled to -0.5 ~ 0.5 (rotation around the vertical axis). |
source | ComputedRef<ParallaxSource> | The input the effect is currently reading from. |