Skip to content

Commit 724c9d1

Browse files
committed
Update logic used to calculate percentage and add new containerRef prop for conditionally calculating status
1 parent e343166 commit 724c9d1

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ In addition to these properties, all other standard React properites like `class
8989
be rendered by the `ScrollTrigger`.
9090

9191
* `component:Element | String` - React component or HTMLElement to render as the wrapper for the `ScrollTrigger` (Default: `div`)
92+
* `containerRef:Object | String` - DOM element instance or string to use for query selecting DOM element. (Default: `document.documentElement`)
9293
* `throttleResize:Number` - Delay to throttle `resize` calls in milliseconds (Default: `100`)
9394
* `throttleScroll:Number` - Delay to throttle `scroll` calls in milliseconds (Default: `100`)
9495
* `triggerOnLoad:Boolean` - Whether or not to trigger the `onEnter` callback on mount (Default: `true`)

src/index.js

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ class ScrollTrigger extends Component {
88
constructor(props) {
99
super(props);
1010

11-
this.onScroll = throttle(this.onScroll.bind(this), props.throttleScroll, {
11+
this.onScrollThrottled = throttle(this.onScroll.bind(this), props.throttleScroll, {
1212
trailing: false,
1313
});
1414

15-
this.onResize = throttle(this.onResize.bind(this), props.throttleResize, {
15+
this.onResizeThrottled = throttle(this.onResize.bind(this), props.throttleResize, {
1616
trailing: false,
1717
});
1818

@@ -25,29 +25,35 @@ class ScrollTrigger extends Component {
2525
}
2626

2727
componentDidMount() {
28-
addEventListener('resize', this.onResize);
29-
addEventListener('scroll', this.onScroll);
28+
addEventListener('resize', this.onResizeThrottled);
29+
addEventListener('scroll', this.onScrollThrottled);
3030

3131
if (this.props.triggerOnLoad) {
3232
this.checkStatus();
3333
}
3434
}
3535

36-
componentWillReceiveProps(nextProps) {
37-
if (nextProps.throttleScroll !== this.props.throttleScroll) {
38-
this.onScroll = throttle(this.onScroll.bind(this, nextProps.throttleScroll));
39-
addEventListener('scroll', this.onScroll);
36+
componentDidUpdate(prevProps, prevState) {
37+
if (prevProps.throttleScroll !== this.props.throttleScroll) {
38+
removeEventListener('scroll', this.onScrollThrottled);
39+
this.onScrollThrottled = throttle(this.onScroll.bind(this), this.props.throttleScroll, {
40+
trailing: false,
41+
});
42+
addEventListener('scroll', this.onScrollThrottled);
4043
}
4144

42-
if (nextProps.throttleResize !== this.props.throttleResize) {
43-
this.onResize = throttle(this.onResize.bind(this, nextProps.throttleResize));
44-
addEventListener('resize', this.onResize);
45+
if (prevProps.throttleResize !== this.props.throttleResize) {
46+
removeEventListener('resize', this.onResizeThrottled);
47+
this.onResizeThrottled = throttle(this.onResize.bind(this), this.props.throttleResize, {
48+
trailing: false,
49+
});
50+
addEventListener('resize', this.onResizeThrottled);
4551
}
4652
}
4753

4854
componentWillUnmount() {
49-
removeEventListener('resize', this.onResize);
50-
removeEventListener('scroll', this.onScroll);
55+
removeEventListener('resize', this.onResizeThrottled);
56+
removeEventListener('scroll', this.onScrollThrottled);
5157
}
5258

5359
onResize() {
@@ -60,6 +66,7 @@ class ScrollTrigger extends Component {
6066

6167
checkStatus() {
6268
const {
69+
containerRef,
6370
onEnter,
6471
onExit,
6572
onProgress,
@@ -73,8 +80,12 @@ class ScrollTrigger extends Component {
7380
const element = ReactDOM.findDOMNode(this.element);
7481
const elementRect = element.getBoundingClientRect();
7582
const viewportStart = 0;
76-
const scrollingElement = document.scrollingElement || document.body;
77-
const viewportEnd = scrollingElement.clientHeight;
83+
const scrollingElement = typeof containerRef === 'string'
84+
? document.querySelector(containerRef)
85+
: containerRef;
86+
const viewportEnd = containerRef === document.documentElement
87+
? Math.max(containerRef.clientHeight, window.innerHeight || 0)
88+
: scrollingElement.clientHeight;
7889
const inViewport = elementRect.top <= viewportEnd && elementRect.bottom >= viewportStart;
7990

8091
const position = window.scrollY;
@@ -154,6 +165,10 @@ ScrollTrigger.propTypes = {
154165
PropTypes.element,
155166
PropTypes.node,
156167
]),
168+
containerRef: PropTypes.oneOfType([
169+
PropTypes.object,
170+
PropTypes.string,
171+
]),
157172
throttleResize: PropTypes.number,
158173
throttleScroll: PropTypes.number,
159174
triggerOnLoad: PropTypes.bool,
@@ -164,6 +179,7 @@ ScrollTrigger.propTypes = {
164179

165180
ScrollTrigger.defaultProps = {
166181
component: 'div',
182+
containerRef: document.documentElement,
167183
throttleResize: 100,
168184
throttleScroll: 100,
169185
triggerOnLoad: true,

0 commit comments

Comments
 (0)