Skip to content

Commit a080a38

Browse files
committed
move _isElementInView function to utils
1 parent 0d70296 commit a080a38

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

src/libs/ParallaxController.js

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {
22
getParallaxOffsets,
3+
isElementInView,
34
parseValueAndUnit,
45
testForPassiveScroll,
56
} from '../utils/index';
@@ -102,7 +103,7 @@ function ParallaxController() {
102103
if (element.props.disabled) return;
103104

104105
// check if the element is in view then
105-
const isInView = _isElementInView(element);
106+
const isInView = isElementInView(element, windowHeight, scrollY);
106107

107108
// set styles if it is
108109
if (isInView) _setParallaxStyles(element);
@@ -266,26 +267,6 @@ function ParallaxController() {
266267
};
267268
}
268269

269-
/**
270-
* Takes a parallax element and returns whether the element
271-
* is in view based on the cached position of the element,
272-
* current scroll position and the window height.
273-
* @param {object} element
274-
* @return {boolean} isInView
275-
*/
276-
function _isElementInView(element) {
277-
const top = element.attributes.top - scrollY;
278-
const bottom = element.attributes.bottom - scrollY;
279-
280-
const topInView = top >= 0 && top <= windowHeight;
281-
const bottomInView = bottom >= 0 && bottom <= windowHeight;
282-
const covering = top <= 0 && bottom >= windowHeight;
283-
284-
const isInView = topInView || bottomInView || covering;
285-
286-
return isInView;
287-
}
288-
289270
/**
290271
* Takes a parallax element and set the styles based on the
291272
* offsets and percent the element has moved though the viewport.

src/utils/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export clamp from './clamp';
22
export getParallaxOffsets from './getParallaxOffsets';
3+
export isElementInView from './isElementInView';
34
export parseValueAndUnit from './parseValueAndUnit';
45
export scaleBetween from './scaleBetween';
56

src/utils/isElementInView.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* Takes a parallax element and returns whether the element
3+
* is in view based on the cached position of the element,
4+
* current scroll position and the window height.
5+
* @param {object} element
6+
* @return {boolean} isInView
7+
*/
8+
export default function isElementInView(element, windowHeight, scrollY) {
9+
const top = element.attributes.top - scrollY;
10+
const bottom = element.attributes.bottom - scrollY;
11+
12+
const topInView = top >= 0 && top <= windowHeight;
13+
const bottomInView = bottom >= 0 && bottom <= windowHeight;
14+
const covering = top <= 0 && bottom >= windowHeight;
15+
16+
const isInView = topInView || bottomInView || covering;
17+
18+
return isInView;
19+
}

0 commit comments

Comments
 (0)