You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-16Lines changed: 36 additions & 16 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# React Scroll Parallax
2
2
3
-
Provides a React component and single global passive scroll listener to add **vertical** scrolling based offsets to elements based on their position in the viewport. Works with universal (server-side rendered) React apps.
3
+
Provides a React component and single passive scroll listener to add **vertical** scrolling based offsets to elements based on their position in the viewport. Works with universal (server-side rendered) React apps.
4
4
5
5
[View on NPM](https://www.npmjs.com/package/react-scroll-parallax)
6
6
@@ -22,7 +22,7 @@ npm i react-scroll-parallax --save
22
22
23
23
Wrap your component tree that will contain `<Parallax />` components with the `<ParallaxProvider />`;
@@ -58,7 +58,7 @@ import { Parallax } from 'react-scroll-parallax';
58
58
</Parallax>
59
59
```
60
60
61
-
**NOTE:** The Parallax Controller caches the scroll state and positions of elements on the page for performance reasons. This means that if the page height changes (perhaps from images loading) after `<Parallax />` components are mounted it won't properly determine when the elements are in view. To correct this you can call the `update()` method from any child component of the `<ParallaxProvider />` via context once every thing has loaded and is ready. More details on how here: [Parallax Controller Context](#parallax-controller-context)
61
+
**NOTE:** The Parallax Controller caches the scroll state and positions of elements on the page for performance reasons. This means that if the page height changes (perhaps from images loading) after `<Parallax />` components are mounted it won't properly determine when the elements are in view. To correct this you can call the `parallaxController.update()` method from any child component of the `<ParallaxProvider />` via context once every thing has loaded and is ready. More details on how here: [Parallax Controller Context](#parallax-controller-context)
62
62
63
63
## Parallax Component Props
64
64
@@ -123,32 +123,32 @@ Optionally pass a tag name to be applied to the outer most parallax element. For
123
123
124
124
Access the Parallax Controller via context in any components rendered within a `<ParallaxProvider />` by defining the `contextTypes` like so:
125
125
126
-
```
126
+
```jsx
127
127
classFooextendsComponent {
128
128
129
129
static contextTypes = {
130
130
parallaxController:PropTypes.object.isRequired,
131
131
};
132
132
133
+
doSomething() {
134
+
// do stuff with this.context.parallaxController
135
+
}
136
+
133
137
...
134
138
```
135
139
136
-
### Available Methods
137
-
138
-
Access the following method via context such as `this.context.parallaxController`.
139
-
140
-
**`update()`**
140
+
or for stateless functional components like:
141
141
142
-
Updates all cached attributes for parallax elements then updates their positions.
143
-
144
-
**`destroy()`**
145
-
146
-
Removes window scroll and resize listeners then resets all styles applied to parallax elements.
142
+
```jsx
143
+
constBar= (props, context) => (
147
144
148
-
## Browser Support
145
+
// do stuff with context.parallaxController
149
146
150
-
React scroll parallax should support the last two versions of all major browsers and has been tested on desktop Chrome, Firefox, Safari and Edge, as well as the following: iOS 9, iOS 10, Android 4 and IE11. If you encounter any errors for browsers that should be supported please post an issue.
147
+
...
148
+
);
151
149
150
+
Bar.contextTypes= {
151
+
parallaxController:PropTypes.object.isRequired,
152
152
## Development
153
153
154
154
Install node modules and start webpack:
@@ -167,6 +167,26 @@ Run Jest tests:
167
167
168
168
`npm run test`
169
169
170
+
};
171
+
172
+
```
173
+
174
+
### Available Methods
175
+
176
+
Access the following methods on `parallaxController` via context:
177
+
178
+
**`update()`**
179
+
180
+
Updates all cached attributes for parallax elements then updates their positions.
181
+
182
+
**`destroy()`**
183
+
184
+
Removes window scroll and resize listeners then resets all styles applied to parallax elements.
185
+
186
+
## Browser Support
187
+
188
+
React scroll parallax should support the last two versions of all major browsers and has been tested on desktop Chrome, Firefox, Safari and Edge, as well as the following: iOS 9, iOS 10, Android 4 and IE11. If you encounter any errors for browsers that should be supported please post an issue.
189
+
170
190
## Optimizations to Reduce Jank
171
191
172
192
React Scroll Parallax uses a single [passive scroll listener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#Improving_scrolling_performance_with_passive_listeners) (dependent on browser support) with the minimal amount of work done on the scroll event to prevent [jank](http://jankfree.org/) (calculations that cause layout, reflow and paint are cached initially and only updated when layout changes). Request animation frame is then used to decouple the scroll handler and further reduce jank. All offsets are applied with 3D transforms to utilize the GPU and prevent paints. If you have ideas to further optimize scrolling please PR or post an issue.
0 commit comments