|
1 | 1 | /* |
2 | 2 | * Responsively Lazy |
3 | 3 | * http://ivopetkov.com/b/lazy-load-responsive-images/ |
4 | | - * Copyright 2015-2016, Ivo Petkov |
| 4 | + * Copyright 2015-2017, Ivo Petkov |
5 | 5 | * Free to use under the MIT license. |
6 | 6 | */ |
7 | 7 |
|
@@ -97,27 +97,29 @@ var responsivelyLazy = (function () { |
97 | 97 | bestSelectedOption = [element.getAttribute('src'), 999999]; |
98 | 98 | } |
99 | 99 |
|
100 | | - if (typeof container.lastSetOption === 'undefined') { |
101 | | - container.lastSetOption = ['', 0]; |
| 100 | + if (typeof container.responsivelyLazyLastSetOption === 'undefined') { |
| 101 | + container.responsivelyLazyLastSetOption = ['', 0]; |
102 | 102 | } |
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; |
105 | 105 | 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 () { |
110 | 109 | var handler = container.getAttribute('data-onlazyload'); |
111 | 110 | if (handler !== null) { |
112 | 111 | (new Function(handler).bind(container))(); |
113 | 112 | } |
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 | + } |
121 | 123 | } |
122 | 124 | }; |
123 | 125 |
|
|
0 commit comments