Skip to content

Commit 3601388

Browse files
committed
Potential fix of double image loading.
1 parent 27bfb56 commit 3601388

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

responsivelyLazy.js

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* Responsively Lazy
33
* http://ivopetkov.com/b/lazy-load-responsive-images/
4-
* Copyright 2015-2016, Ivo Petkov
4+
* Copyright 2015-2017, Ivo Petkov
55
* Free to use under the MIT license.
66
*/
77

@@ -97,27 +97,29 @@ var responsivelyLazy = (function () {
9797
bestSelectedOption = [element.getAttribute('src'), 999999];
9898
}
9999

100-
if (typeof container.lastSetOption === 'undefined') {
101-
container.lastSetOption = ['', 0];
100+
if (typeof container.responsivelyLazyLastSetOption === 'undefined') {
101+
container.responsivelyLazyLastSetOption = ['', 0];
102102
}
103-
if (container.lastSetOption[1] < bestSelectedOption[1]) {
104-
var fireEvent = container.lastSetOption[1] === 0;
103+
if (container.responsivelyLazyLastSetOption[1] < bestSelectedOption[1]) {
104+
container.responsivelyLazyLastSetOption = bestSelectedOption;
105105
var url = bestSelectedOption[0];
106-
var image = new Image();
107-
image.addEventListener('load', function () {
108-
element.setAttribute('srcset', url);
109-
if (fireEvent) {
106+
if (typeof container.responsivelyLazyEventsAttached === 'undefined') {
107+
container.responsivelyLazyEventsAttached = true;
108+
element.addEventListener('load', function () {
110109
var handler = container.getAttribute('data-onlazyload');
111110
if (handler !== null) {
112111
(new Function(handler).bind(container))();
113112
}
114-
}
115-
}, false);
116-
image.addEventListener('error', function () {
117-
container.lastSetOption = ['', 0];
118-
}, false);
119-
image.src = url;
120-
container.lastSetOption = bestSelectedOption;
113+
}, false);
114+
element.addEventListener('error', function () {
115+
container.responsivelyLazyLastSetOption = ['', 0];
116+
}, false);
117+
}
118+
if (url === element.getAttribute('src')) {
119+
element.removeAttribute('srcset');
120+
} else {
121+
element.setAttribute('srcset', url);
122+
}
121123
}
122124
};
123125

responsivelyLazy.min.js

Lines changed: 9 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)