useParallax
Functions usePositiveOffset
, useSticky
, useSpeed
and useTrigger
are custom hook that I wrote to make it easier to create common parallax effect such as sticky headers.
usePositiveOffset
Description
Convert a negative offset MotionValue
, that comes with contentOffsetX
and contentOffsetY
props of Scroll
, to a positive one.
Usage
import { usePositiveOffset } from "./useParallax"
...
function App() {
const positiveOffset = usePositiveOffset(offset)
...
}
useSticky
Description
Returns a MotionValue
which can be used to make an item sticky when the Scroll
is scrolled into specific ranges.
Usage
import { useSticky } from "./useParallax"
...
function App() {
...
const titleOffset = useSticky(positiveOffset, [200, 400], [500, 700], [900, 1200])
...
return (
<Scroll contentOffsetY={offset}>
// ==> 3. use titleOffset as a prop
<Frame y={titleOffset}>Title</Frame>
...
</Scroll>
)
}
In the code above, the title will be sticky when the list is scrolled into three ranges. When the list is scrolled outside of these ranges, the title will scroll with the rest of list normally.
useSpeed
Description
Returns a MotionValue
which can be used to make an item move at a different speed when the Scroll
is scrolled into specified ranges.
Usage
import { useSpeed } from "./useParallax"
...
function App() {
...
const titleOffset = useSpeed(positiveOffset,
[0, 200 ], 1,
[200, 400], 0,
[500, 700], -0.5,
[900, 1200], -1,
[1200, 1500], -2
)
...
return (
<Scroll contentOffsetY={offset}>
// ==> 3. use titleOffset as a prop
<Frame y={titleOffset}>Title</Frame>
...
</Scroll>
)
}
useTrigger
Description
Trigger something, e.g. an animation, when scrolling into the specified range
Usage
import { useTrigger } from "./useParallax"
...
function App() {
...
useTrigger(
positiveOffset,
[200, 300],
function(direction) {
if (direction > 0) anim.start({ rotate: 180 })
else anim.start({ rotate: 0 })
}
)
...
}