Skip to content

Commit f699c71

Browse files
committed
docs: add horizontal scroll example
1 parent 96fdb52 commit f699c71

File tree

2 files changed

+103
-0
lines changed

2 files changed

+103
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
sidebar_position: 6
3+
---
4+
5+
import { ExampleHorizontalScroll } from '/src/components/example-horizontal-scroll';
6+
7+
# Horizontal Scroll
8+
9+
The controller isn't limited to the typical vertical scrolling. To setup a horizontal scrolling document you'll need to set the `scrollAxis` in the [`<ParallaxProvider>`](/docs/usage/components/parallax-provider) to `horizontal`.
10+
11+
```tsx
12+
const App = () => (
13+
<ParallaxProvider scrollAxis="horizontal">
14+
<HorizontalContainer />
15+
</ParallaxProvider>
16+
);
17+
```
18+
19+
Once the provider is setup, you can start creating effects as usual. Here's an example that uses a few layers with repeating backgrounds.
20+
21+
:::info
22+
23+
"Demon Woods" parallax art by [Aethrall](https://aethrall.itch.io/)
24+
25+
:::
26+
27+
<ExampleHorizontalScroll />
28+
29+
:::note
30+
31+
[Open the above example](https://react-scroll-parallax-horizontal-scroll.surge.sh/) or view the entire [source code](https://github.com/jscottsmith/react-scroll-parallax-examples/tree/master/horizontal-scroll).
32+
33+
:::
34+
35+
## Source Code
36+
37+
To achieve the above effect, layers are stacked absolutely in a container. A `speed` is assigned to the middle and closest tree images - the closest moves the fastest. Then the `left` and `right` are extended so the edges are never seen. A `targetElement` is also assigned to the containing `div` and passed to each parallax layer so that the progress rate is calculated equally. Here's the setup:
38+
39+
```tsx
40+
function DemonWoods() {
41+
const target = useRef(null);
42+
43+
const mid = useParallax({
44+
speed: 50,
45+
targetElement: target.current,
46+
});
47+
const close = useParallax({
48+
speed: 100,
49+
targetElement: target.current,
50+
});
51+
52+
const midExtend = 50 * 5 * -1;
53+
const closeExtend = 100 * 5 * -1;
54+
55+
return (
56+
<div className={styles.root} ref={target}>
57+
<div
58+
style={{ backgroundImage: `url(${imageBg})`, left: 0, right: 0 }}
59+
className={styles.layer}
60+
/>
61+
<div
62+
style={{
63+
backgroundImage: `url(${imageFar})`,
64+
left: 0,
65+
right: 0,
66+
}}
67+
className={styles.layer}
68+
/>
69+
<div
70+
style={{
71+
backgroundImage: `url(${imageMid})`,
72+
left: midExtend,
73+
right: midExtend,
74+
}}
75+
className={styles.layer}
76+
ref={mid.ref}
77+
/>
78+
<div
79+
style={{
80+
backgroundImage: `url(${imageClose})`,
81+
left: closeExtend,
82+
right: closeExtend,
83+
}}
84+
className={styles.layer}
85+
ref={close.ref}
86+
/>
87+
</div>
88+
);
89+
}
90+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React from 'react';
2+
3+
export const ExampleHorizontalScroll = () => {
4+
return (
5+
<div className="mb-md">
6+
<iframe
7+
src="https://react-scroll-parallax-horizontal-scroll.surge.sh/"
8+
style={{ aspectRatio: '1.5 / 1' }}
9+
width="100%"
10+
/>
11+
</div>
12+
);
13+
};

0 commit comments

Comments
 (0)