Skip to content

Commit bd482d8

Browse files
committed
Add support for horizontal scroll
1 parent 886f4ba commit bd482d8

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

src/components/LazyLoadImage.jsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,24 @@ class LazyLoadImage extends React.Component {
6363
const boundingBox = {
6464
bottom: this.refs.placeholder.offsetTop +
6565
this.refs.placeholder.offsetHeight,
66+
left: this.refs.placeholder.offsetLeft,
67+
right: this.refs.placeholder.offsetLeft +
68+
this.refs.placeholder.offsetWidth,
6669
top: this.refs.placeholder.offsetTop
6770
};
6871
const viewport = {
69-
bottom: scrollPosition + window.innerHeight,
70-
top: scrollPosition
72+
bottom: scrollPosition.y + window.innerHeight,
73+
left: scrollPosition.x,
74+
right: scrollPosition.x + window.innerWidth,
75+
top: scrollPosition.y
7176
};
7277

7378
this.previousBoundingBox = boundingBox;
7479

7580
return Boolean(viewport.top - threshold <= boundingBox.bottom &&
76-
viewport.bottom + threshold >= boundingBox.top);
81+
viewport.bottom + threshold >= boundingBox.top &&
82+
viewport.left - threshold <= boundingBox.right &&
83+
viewport.right + threshold >= boundingBox.left);
7784
}
7885

7986
getPlaceholder() {
@@ -108,7 +115,10 @@ class LazyLoadImage extends React.Component {
108115
}
109116

110117
LazyLoadImage.propTypes = {
111-
scrollPosition: PropTypes.number.isRequired,
118+
scrollPosition: PropTypes.shape({
119+
x: PropTypes.number.isRequired,
120+
y: PropTypes.number.isRequired
121+
}).isRequired,
112122
afterLoad: PropTypes.func,
113123
beforeLoad: PropTypes.func,
114124
className: PropTypes.string,

src/components/LazyLoadImage.spec.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('LazyLoadImage', function() {
1414
afterLoad = () => null,
1515
beforeLoad = () => null,
1616
placeholder = null,
17-
scrollPosition = 0
17+
scrollPosition = {x: 0, y: 0}
1818
} = {}) {
1919
return ReactTestUtils.renderIntoDocument(
2020
<LazyLoadImage
@@ -40,7 +40,7 @@ describe('LazyLoadImage', function() {
4040

4141
it('renders the default placeholder when it\'s not in the viewport', function() {
4242
const lazyLoadImage = renderLazyLoadImage({
43-
scrollPosition: -1000
43+
scrollPosition: {x: 0, y: -1000}
4444
});
4545

4646
expectImages(lazyLoadImage, 0);
@@ -50,7 +50,7 @@ describe('LazyLoadImage', function() {
5050
it('renders the prop placeholder when it\'s not in the viewport', function() {
5151
const placeholder = <span className="test-placeholder"></span>;
5252
const lazyLoadImage = renderLazyLoadImage({
53-
scrollPosition: -1000,
53+
scrollPosition: {x: 0, y: -1000},
5454
placeholder
5555
});
5656

@@ -67,10 +67,10 @@ describe('LazyLoadImage', function() {
6767

6868
it('renders the image when it appears in the viewport', function() {
6969
const lazyLoadImage = renderLazyLoadImage({
70-
scrollPosition: -1000
70+
scrollPosition: {x: 0, y: -1000}
7171
});
7272

73-
lazyLoadImage.componentWillReceiveProps({scrollPosition: 0});
73+
lazyLoadImage.componentWillReceiveProps({scrollPosition: {x: 0, y: 0}});
7474

7575
expectImages(lazyLoadImage, 1);
7676
expectPlaceholders(lazyLoadImage, 0);
@@ -80,7 +80,7 @@ describe('LazyLoadImage', function() {
8080
const beforeLoad = jest.fn();
8181
const lazyLoadImage = renderLazyLoadImage({
8282
beforeLoad,
83-
scrollPosition: -1000
83+
scrollPosition: {x: 0, y: -1000}
8484
});
8585

8686
expect(beforeLoad).toHaveBeenCalledTimes(0);
@@ -99,10 +99,10 @@ describe('LazyLoadImage', function() {
9999
const beforeLoad = jest.fn();
100100
const lazyLoadImage = renderLazyLoadImage({
101101
beforeLoad,
102-
scrollPosition: -1000
102+
scrollPosition: {x: 0, y: -1000}
103103
});
104104

105-
lazyLoadImage.componentWillReceiveProps({scrollPosition: 0});
105+
lazyLoadImage.componentWillReceiveProps({scrollPosition: {x: 0, y: 0}});
106106

107107
expect(beforeLoad).toHaveBeenCalledTimes(1);
108108
});
@@ -111,7 +111,7 @@ describe('LazyLoadImage', function() {
111111
const afterLoad = jest.fn();
112112
const lazyLoadImage = renderLazyLoadImage({
113113
afterLoad,
114-
scrollPosition: -1000
114+
scrollPosition: {x: 0, y: -1000}
115115
});
116116

117117
expect(afterLoad).toHaveBeenCalledTimes(0);
@@ -130,10 +130,10 @@ describe('LazyLoadImage', function() {
130130
const afterLoad = jest.fn();
131131
const lazyLoadImage = renderLazyLoadImage({
132132
afterLoad,
133-
scrollPosition: -1000
133+
scrollPosition: {x: 0, y: -1000}
134134
});
135135

136-
lazyLoadImage.componentWillReceiveProps({scrollPosition: 0});
136+
lazyLoadImage.componentWillReceiveProps({scrollPosition: {x: 0, y: 0}});
137137

138138
expect(afterLoad).toHaveBeenCalledTimes(1);
139139
});

src/hoc/trackWindowScroll.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ const trackWindowScroll = (BaseComponent) => {
66
super(props);
77

88
this.state = {
9-
scrollPosition: window.scrollY || window.pageYOffset
9+
scrollPosition: {
10+
x: window.scrollX || window.pageXOffset,
11+
y: window.scrollY || window.pageYOffset
12+
}
1013
};
1114
}
1215

@@ -22,7 +25,10 @@ const trackWindowScroll = (BaseComponent) => {
2225

2326
onChangeScroll() {
2427
this.setState({
25-
scrollPosition: window.scrollY || window.pageYOffset
28+
scrollPosition: {
29+
x: window.scrollX || window.pageXOffset,
30+
y: window.scrollY || window.pageYOffset
31+
}
2632
});
2733
}
2834

@@ -39,4 +45,3 @@ const trackWindowScroll = (BaseComponent) => {
3945
};
4046

4147
export default trackWindowScroll;
42-

0 commit comments

Comments
 (0)