|
1 | 1 | # @react-spring/parallax |
2 | 2 |
|
3 | | -This package exports the `Parallax` and `ParallaxLayer` components. Both are wrapped with `React.memo`. |
| 3 | +```bash |
| 4 | +yarn add @react-spring/parallax |
| 5 | +``` |
4 | 6 |
|
5 | | -`Parallax` creates a scroll container. Throw in any amount of `ParallaxLayer`s and it will take care of |
6 | | -moving them in accordance to their offsets and speeds. |
| 7 | +**NOTE**: Currently, only `@react-spring/web` is supported. |
7 | 8 |
|
8 | | -**Note:** Currently, only `@react-spring/web` is supported. |
| 9 | +`Parallax` creates a scrollable container. `ParallaxLayer`s contain your content and will be moved according to their offsets and speeds. |
9 | 10 |
|
10 | | -```tsx |
| 11 | +```jsx |
11 | 12 | import { Parallax, ParallaxLayer } from '@react-spring/parallax' |
12 | 13 |
|
13 | 14 | const Example = () => { |
14 | | - const ref = useRef<Parallax>() |
| 15 | + const ref = useRef() |
15 | 16 | return ( |
16 | | - <Parallax ref={ref} pages={3} scrolling={false} horizontal> |
17 | | - <ParallaxLayer offset={0} speed={0.5}> |
18 | | - <span |
19 | | - onClick={() => { |
20 | | - ref.current.scrollTo(1) |
21 | | - }}> |
22 | | - Layers can contain anything |
23 | | - </span> |
| 17 | + <Parallax pages={3} ref={ref}> |
| 18 | + <ParallaxLayer offset={0} speed={2.5}> |
| 19 | + <p>Layers can contain anything</p> |
| 20 | + </ParallaxLayer> |
| 21 | + |
| 22 | + <ParallaxLayer offset={1} speed={-2} factor={1.5} horizontal /> |
| 23 | + |
| 24 | + <ParallaxLayer sticky={{ start: 1, end: 2 }} /> |
| 25 | + |
| 26 | + <ParallaxLayer offset={2} speed={1}> |
| 27 | + <button onClick={() => ref.current.scrollTo(0)}>Scroll to top</button> |
24 | 28 | </ParallaxLayer> |
25 | 29 | </Parallax> |
26 | 30 | ) |
27 | 31 | } |
28 | 32 | ``` |
29 | 33 |
|
30 | | -## `Parallax` props |
31 | | - |
32 | | -- `pages: number` |
33 | | - |
34 | | - Determines the total space of the inner content where each page takes 100% of the visible container. |
35 | | - |
36 | | -- `config?: SpringConfig` |
37 | | - |
38 | | - The spring behavior. |
39 | | - |
40 | | - Defaults to `config.slow`. |
41 | | - |
42 | | -- `scrolling?: boolean` |
43 | | - |
44 | | - Allow content to be scrolled or not. |
45 | | - |
46 | | - Defaults to `true`. |
47 | | - |
48 | | -- `horizontal?: boolean |
49 | | - |
50 | | - When `true`, content scrolls from left to right. |
51 | | - |
52 | | - Defaults to `false`. |
53 | | - |
54 | | -## `ParallaxLayer` props |
| 34 | +## Parallax |
55 | 35 |
|
56 | | -- `factor?: number` |
| 36 | +| Property | Type | Description | |
| 37 | +| ----------- | ------------- | ------------------------------------------------------------------------------------------------------- | |
| 38 | +| pages | number | Total space of the container. Each page takes up 100% of the viewport. | |
| 39 | +| config? | SpringConfig | The spring behavior. Defaults to `config.slow` (see [configs](https://react-spring.io/common/configs)). | |
| 40 | +| enabled? | boolean | Whether or not the content can be scrolled. Defaults to `true`. | |
| 41 | +| horizontal? | boolean | Whether or not the container scrolls horizontally. Defaults to `false`. | |
| 42 | +| innerStyle? | CSSProperties | CSS object to style the inner `Parallax` wrapper (not the scrollable container) | |
57 | 43 |
|
58 | | - The page size (eg: 1 => 100%, 1.5 => 150%, etc) |
| 44 | +### `scrollTo` |
59 | 45 |
|
60 | | - Defaults to `1`. |
| 46 | +`Parallax` also has a `scrollTo` function for click-to-scroll. It takes one paramater: the number of the page to scroll to |
| 47 | +(Pages are zero-indexed so `scrollTo(0)` will scroll to the first page, etc). |
61 | 48 |
|
62 | | -- `offset?: number` |
| 49 | +### Usage Notes |
63 | 50 |
|
64 | | - The page offset (eg: 0 => top of 1st page, 1 => top of 2nd page, etc) |
| 51 | +- `Parallax` is a scrollable container so all scroll events are fired from the container itself -- listening for scroll on `window` won't work. |
| 52 | +- `scrollTo` is a method on `Parallax` -- you access it with a `ref` and then `ref.current.scrollTo(pageNumber)`. |
65 | 53 |
|
66 | | - Defaults to `0`. |
| 54 | +## ParallaxLayer |
67 | 55 |
|
68 | | -- `speed?: number` |
| 56 | +| Property | Type | Description | |
| 57 | +| ----------- | ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 58 | +| factor? | number | Size of the layer relative to page size (eg: `1` => 100%, `1.5` => 150%, etc). Defaults to `1`. | |
| 59 | +| offset? | number | The offset of the layer when it's corresponding page is fully in view (eg: `0` => top of 1st page, `1` => top of 2nd page, etc ). Defaults to `0`. | |
| 60 | +| speed? | number | Rate at which the layer moves in relation to scroll. Can be positive or negative. Defaults to `0`. | |
| 61 | +| horizontal? | boolean | Whether or not the layer moves horizontally. Defaults to the `horizontal` value of `Parallax` (whose default is `false`). | |
| 62 | +| sticky? | StickyConfig | If set, the layer will be 'sticky' between the two offsets. All other props are ignored. Default: `{start?: number = 0, end?: number = start + 1}` | |
69 | 63 |
|
70 | | - Shift the layer in accordance to its offset. Values can be positive or negative. |
| 64 | +### Usage Notes |
71 | 65 |
|
72 | | - Defaults to `0`. |
| 66 | +- The `offset` prop is where the layer will end up, not where it begins. For example, if a layer has an offset of `1.5`, it will be halfway down the second page (zero-indexed) when the second page completely fills the viewport. |
| 67 | +- The `speed` prop will affect the initial starting position of a layer, but not it's final `offset` position. |
| 68 | +- Any layer with `sticky` set will have a `z-index` higher than regular layers. This can be changed manually. |
73 | 69 |
|
74 | | -## Credits |
| 70 | +## Demos |
75 | 71 |
|
76 | | -Paul Henschel |
| 72 | +- [Parallax - vertical](https://codesandbox.io/s/github/pmndrs/react-spring/tree/master/demo/src/sandboxes/parallax-vert) |
| 73 | +- [Parallax - horizontal](https://codesandbox.io/s/github/pmndrs/react-spring/tree/master/demo/src/sandboxes/parallax) |
| 74 | +- [Parallax - sticky](https://codesandbox.io/s/github/pmndrs/react-spring/tree/master/demo/src/sandboxes/parallax-sticky) |
0 commit comments