Skip to content

Commit eda2875

Browse files
committed
Fix loading not visible images and images with unknown sizes.
1 parent e25feea commit eda2875

File tree

3 files changed

+14
-12
lines changed

3 files changed

+14
-12
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "responsively-lazy",
33
"title": "Responsively Lazy",
4-
"version": "3.0.0",
4+
"version": "3.0.1",
55
"description": "Lazy load responsive images and HTML",
66
"homepage": "https://ivopetkov.com/responsively-lazy/",
77
"author": {

responsively-lazy.js

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,21 @@ var responsivelyLazy = typeof responsivelyLazy !== 'undefined' ? responsivelyLaz
4040
var elementLeft = rect.left;
4141
var elementWidth = rect.width;
4242
var elementHeight = rect.height;
43+
if (elementTop === 0 && elementLeft === 0 && elementWidth === 0 && elementHeight === 0) {
44+
return 0;
45+
}
46+
if (elementWidth === 0) {
47+
elementWidth = 1;
48+
}
49+
if (elementHeight === 0) {
50+
elementHeight = 1;
51+
}
4352

44-
var getVisiblitySize = function (elementPoint, elementSize, windowSize) {
53+
var getVisibleAreaSize = function (elementPoint, elementSize, windowSize) {
4554
return elementPoint < windowSize && elementPoint + elementSize > 0 ? Math.min(windowSize, elementPoint + elementSize) - Math.max(0, elementPoint) : 0;
4655
};
4756

48-
var percent = (getVisiblitySize(elementLeft, elementWidth, windowWidth) * getVisiblitySize(elementTop, elementHeight, windowHeight)) / (elementWidth * elementHeight) * 100;
49-
if (percent === 0) {
50-
if (thresholdVertical > 0 || thresholdHorizontal > 0) {
51-
percent = (getVisiblitySize(elementLeft - thresholdHorizontal, elementWidth + 2 * thresholdHorizontal, windowWidth) * getVisiblitySize(elementTop - thresholdVertical, elementHeight + 2 * thresholdVertical, windowHeight)) / ((elementWidth + 2 * thresholdHorizontal) * (elementHeight + 2 * thresholdVertical));
52-
}
53-
}
54-
return percent;
57+
return (getVisibleAreaSize(elementLeft - thresholdHorizontal, elementWidth + 2 * thresholdHorizontal, windowWidth) * getVisibleAreaSize(elementTop - thresholdVertical, elementHeight + 2 * thresholdVertical, windowHeight)) / ((elementWidth + 2 * thresholdHorizontal) * (elementHeight + 2 * thresholdVertical)) * 100;
5558
}
5659

5760
var evalScripts = function (scripts, startIndex) {
@@ -203,8 +206,7 @@ var responsivelyLazy = typeof responsivelyLazy !== 'undefined' ? responsivelyLaz
203206
options = temp;
204207
}
205208
}
206-
var preferredOption = element.getAttribute('data-responsively-lazy-preferred-option');
207-
var elementWidth = preferredOption !== null ? parseInt(preferredOption) : element.offsetWidth * (typeof window.devicePixelRatio !== 'undefined' ? window.devicePixelRatio : 1);
209+
var elementWidth = element.offsetWidth * (typeof window.devicePixelRatio !== 'undefined' ? window.devicePixelRatio : 1);
208210

209211
var bestSelectedOption = null;
210212
for (var j = 0; j < options.length; j++) {

responsively-lazy.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)