forked from swordensen/useful-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimages-defer.js
More file actions
42 lines (36 loc) · 1.06 KB
/
images-defer.js
File metadata and controls
42 lines (36 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function loadImages() {
// images
$('img, picture > source').each(function () {
var img = $(this),
imgSrc = $(this).data('src') || $(this).attr('src'),
imgSrcset = $(this).data('srcset');
(imgSrc !== 'undefined') && $(this).attr('src', imgSrc);
(imgSrcset !== 'undefined') && $(this).attr('srcset', imgSrcset);
if($(this).hasClass('svg')){
var imgID = img.attr('id');
var imgClass = img.attr('class');
$.get(imgSrc, function(data) {
var svg = $(data).find('svg');
if (typeof imgID !== 'undefined') {
svg = svg.attr('id', imgID);
}
if (typeof imgClass !== 'undefined') {
svg = svg.attr('class', imgClass + ' replaced-svg');
}
svg = svg.removeAttr('xmlns:a');
img.replaceWith(svg);
}, 'xml');
}
});
// background images
$('div, section').each(function () {
if ($(this).attr('data-src')) {
var backgroundImg = $(this).data('src');
$(this).css('background-image', 'url(' + backgroundImg + ')');
}
});
console.log('images loaded');
//css images
$('html').addClass('lazy-loaded');
}
loadImages();