-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresponsive-images.js
68 lines (65 loc) · 1.72 KB
/
responsive-images.js
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
if( 'undefined' == typeof WhatCheer ) { window.WhatCheer = {}; }
WhatCheer.Responsive = {
getWindowWidth: function () {
var width = null;
// IE
if (document.body && document.body.offsetWidth) {
width = document.body.offsetWidth;
}
if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth) {
width = document.documentElement.offsetWidth;
}
// Everyone else
if (window.innerWidth && window.innerHeight) {
width = window.innerWidth;
}
return width;
},
updateImages: function () {
var width = WhatCheer.Responsive.getWindowWidth(),
images = document.getElementsByTagName( 'img' ),
format = null,
breaks = null,
size = null,
name = null,
path = null,
i = 0,
j = 0;
for( i = 0; i < images.length; ++i ) {
format = images[i].getAttribute('data-format');
breaks = images[i].getAttribute('data-breaks');
if( format && breaks ) {
breaks = breaks.split(';');
for( j = 0; j < breaks.length; ++j ) {
if( -1 == breaks[j].indexOf( ':' ) ) {
size = parseInt( breaks[j] );
name = breaks[j];
}
else {
size = parseInt( breaks[j].slice( 0, breaks[j].indexOf( ':' ) ) )
name = breaks[j].slice( breaks[j].indexOf( ':' ) + 1 )
}
if( size < width ) {
path = format.replace( '{s}', name );
if( images[i].src != path ) {
( function () {
var tmp = new Image(),
img = images[i],
src = path;
tmp.onload = function () {
img.src = src;
delete tmp;
}
tmp.onerror = function () {
delete tmp;
}
tmp.src = src;
} ).call();
}
break;
}
}
}
}
}
};