Skip to content

Commit 68aaceb

Browse files
committed
Fix images not loading when appearing after a horizontal position change
1 parent 22b695a commit 68aaceb

File tree

2 files changed

+26
-13
lines changed

2 files changed

+26
-13
lines changed

src/components/LazyLoadImage.jsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,24 @@ class LazyLoadImage extends React.Component {
5959
return false;
6060
}
6161

62+
getPlaceholderBoundingBox() {
63+
return {
64+
bottom: this.placeholder.offsetTop +
65+
this.placeholder.offsetHeight,
66+
left: this.placeholder.offsetLeft,
67+
right: this.placeholder.offsetLeft +
68+
this.placeholder.offsetWidth,
69+
top: this.placeholder.offsetTop
70+
};
71+
}
72+
6273
componentDidUpdate() {
6374
if (this.placeholder) {
64-
const boundingBox = {
65-
bottom: this.placeholder.offsetTop +
66-
this.placeholder.offsetHeight,
67-
top: this.placeholder.offsetTop
68-
};
75+
const boundingBox = this.getPlaceholderBoundingBox();
6976

7077
if (this.previousBoundingBox.bottom !== boundingBox.bottom ||
78+
this.previousBoundingBox.left !== boundingBox.left ||
79+
this.previousBoundingBox.right !== boundingBox.right ||
7180
this.previousBoundingBox.top !== boundingBox.top) {
7281
this.updateVisibility();
7382
}
@@ -96,14 +105,7 @@ class LazyLoadImage extends React.Component {
96105
}
97106

98107
const { threshold } = this.props;
99-
const boundingBox = {
100-
bottom: this.placeholder.offsetTop +
101-
this.placeholder.offsetHeight,
102-
left: this.placeholder.offsetLeft,
103-
right: this.placeholder.offsetLeft +
104-
this.placeholder.offsetWidth,
105-
top: this.placeholder.offsetTop
106-
};
108+
const boundingBox = this.getPlaceholderBoundingBox();
107109
const viewport = {
108110
bottom: scrollPosition.y + window.innerHeight,
109111
left: scrollPosition.x,

src/components/LazyLoadImage.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ describe('LazyLoadImage', function() {
7676
expectPlaceholders(lazyLoadImage, 0);
7777
});
7878

79+
it('renders the image when it appears in the viewport horizontally', function() {
80+
const lazyLoadImage = renderLazyLoadImage({
81+
scrollPosition: {x: -1000, y: 0}
82+
});
83+
84+
lazyLoadImage.componentWillReceiveProps({scrollPosition: {x: 0, y: 0}});
85+
86+
expectImages(lazyLoadImage, 1);
87+
expectPlaceholders(lazyLoadImage, 0);
88+
});
89+
7990
it('doesn\'t trigger beforeLoad when the image is not the viewport', function() {
8091
const beforeLoad = jest.fn();
8192
const lazyLoadImage = renderLazyLoadImage({

0 commit comments

Comments
 (0)