Skip to content

Commit dd2c1b8

Browse files
committed
Fix support of lazyload/lazyerror events in srcset addon (issue ressio#2)
1 parent 83e544d commit dd2c1b8

File tree

1 file changed

+52
-40
lines changed

1 file changed

+52
-40
lines changed

src/jquery.lazyloadxt.srcset.js

Lines changed: 52 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -49,53 +49,65 @@
4949
return item[property] === limit;
5050
}
5151

52+
function parseSrcset($el) {
53+
var srcset = $el.attr(options.srcsetAttr);
54+
55+
if (!srcset) {
56+
return false;
57+
}
58+
59+
var list = srcset.split(',').map(function (item) {
60+
return {
61+
url: reUrl.exec(item)[1],
62+
w: parseFloat((reWidth.exec(item) || infty)[1]),
63+
h: parseFloat((reHeight.exec(item) || infty)[1]),
64+
x: parseFloat((reDpr.exec(item) || one)[1])
65+
};
66+
});
67+
68+
if (!list.length) {
69+
return false;
70+
}
71+
72+
var documentElement = document.documentElement,
73+
whx,
74+
src;
75+
76+
viewport = {
77+
w: window.innerWidth || documentElement.clientWidth,
78+
h: window.innerHeight || documentElement.clientHeight,
79+
x: window.devicePixelRatio || 1
80+
};
81+
82+
for (whx in viewport) {
83+
property = whx;
84+
limit = mathFilter(list, 'max');
85+
list = $.grep(list, compareMax);
86+
}
87+
88+
for (whx in viewport) {
89+
property = whx;
90+
limit = mathFilter(list, 'min');
91+
list = $.grep(list, compareMin);
92+
}
93+
94+
src = list[0].url;
95+
96+
if (options.srcsetExtended) {
97+
src = ($el.attr(options.srcsetBaseAttr) || '') + src + ($el.attr(options.srcsetExtAttr) || '');
98+
}
99+
100+
return src;
101+
}
102+
52103
$(document).on('lazyshow', 'img', function (e, $el) {
53104
var srcset = $el.attr(options.srcsetAttr);
54105

55106
if (srcset) {
56107
if (!options.srcsetExtended && srcsetSupport) {
57108
$el.attr('srcset', srcset);
58109
} else {
59-
var list = srcset.split(',').map(function (item) {
60-
return {
61-
url: reUrl.exec(item)[1],
62-
w: parseFloat((reWidth.exec(item) || infty)[1]),
63-
h: parseFloat((reHeight.exec(item) || infty)[1]),
64-
x: parseFloat((reDpr.exec(item) || one)[1])
65-
};
66-
});
67-
68-
if (list.length) {
69-
var documentElement = document.documentElement,
70-
whx,
71-
src;
72-
73-
viewport = {
74-
w: window.innerWidth || documentElement.clientWidth,
75-
h: window.innerHeight || documentElement.clientHeight,
76-
x: window.devicePixelRatio || 1
77-
};
78-
79-
for (whx in viewport) {
80-
property = whx;
81-
limit = mathFilter(list, 'max');
82-
list = $.grep(list, compareMax);
83-
}
84-
85-
for (whx in viewport) {
86-
property = whx;
87-
limit = mathFilter(list, 'min');
88-
list = $.grep(list, compareMin);
89-
}
90-
91-
src = list[0].url;
92-
93-
if (options.srcsetExtended) {
94-
src = ($el.attr(options.srcsetBaseAttr) || '') + src + ($el.attr(options.srcsetExtAttr) || '');
95-
}
96-
97-
$el.attr('src', src);
98-
}
110+
$el.lazyLoadXT.srcAttr = parseSrcset;
99111
}
100112
}
101113
});

0 commit comments

Comments
 (0)