-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bb46212
commit a2c38c7
Showing
6 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
sidebar_position: 3 | ||
--- | ||
|
||
import { CustomEffect } from '/src/components/custom-effect'; | ||
|
||
# Custom Effects | ||
|
||
You can create your own custom effects by using the `onProgressChange` event, along with custom CSS variables. This allows the for animating any CSS property based on the scroll position of an element. | ||
|
||
## CSS Variable Examples | ||
|
||
With the `useParallax` hook, we can pass in a callback to the `onProgressChange` event. This callback will be called every time the scroll position changes. We can then use the `progress` value to update a CSS variable. Then, either in a CSS file or inline styles, we can use the `var(--progress)` to animate any CSS property. | ||
|
||
```tsx | ||
function Example() { | ||
const parallax = useParallax({ | ||
onProgressChange: (progress) => { | ||
if (parallax.ref.current) { | ||
// set progress to CSS variable | ||
parallax.ref.current.style.setProperty( | ||
"--progress", | ||
progress.toString() | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
return ( | ||
<h1 | ||
ref={parallax.ref} | ||
className="text-stroke" | ||
// use the progress variable to change the width of the stroke as progress updates | ||
style={{ textStrokeWidth: `calc(20px * var(--progress))` }} | ||
> | ||
Hello World | ||
</h1> | ||
); | ||
} | ||
``` | ||
|
||
Using this technique, we can animate any CSS property based on the scroll position of an element. Here's another example of animating the letter spacing and text shadow of a text element: | ||
|
||
<CustomEffect/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 5 | ||
sidebar_position: 7 | ||
--- | ||
|
||
import { EasingDemo } from '/src/components/easing-demo'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
.text-stroke { | ||
font-family: sans-serif; | ||
font-size: 15vmin; | ||
height: 100px; | ||
|
||
-webkit-text-stroke-color: white; | ||
-webkit-text-stroke-width: 1px; | ||
|
||
/* color:paleturquoise; */ | ||
@apply text-blue-400; | ||
letter-spacing: calc(2vw * var(--progress) - 1vw); | ||
white-space: nowrap; | ||
text-shadow: 0 calc(4vw * var(--progress) - 2vw) 0 rgba(156, 163, 175, 0.2); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
import { useParallax } from 'react-scroll-parallax'; | ||
import { BgContainer } from '../bg-container'; | ||
import './index.css'; | ||
|
||
export function CustomEffect() { | ||
const parallax = useParallax<HTMLHeadingElement>({ | ||
onProgressChange: (progress) => { | ||
if (parallax.ref.current) { | ||
// set progress to CSS variable | ||
parallax.ref.current.style.setProperty( | ||
'--progress', | ||
progress.toString() | ||
); | ||
} | ||
}, | ||
}); | ||
return ( | ||
<BgContainer> | ||
<div className="flex items-center justify-center"> | ||
<h1 ref={parallax.ref} className="text-stroke"> | ||
Hello World | ||
</h1> | ||
</div> | ||
</BgContainer> | ||
); | ||
} |