-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponsiveImages.js
More file actions
137 lines (123 loc) · 4.52 KB
/
Copy pathresponsiveImages.js
File metadata and controls
137 lines (123 loc) · 4.52 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
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/**
* Responsive Image Plugin
* @version 1.0.0
* @author Dominic Adams
* @license The MIT License (MIT)
*/
function makeImagesResponsive(){
var viewport = window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
/*============================
GET ALL IMAGES
=============================*/
var images = document.getElementsByTagName('body')[0].getElementsByTagName('img');
if( images.length === 0 ){
return;
}
/*============================
HAS ATTR FUNCTION
=============================*/
var hasAttr;
if(!images[0].hasAttribute){ //IE <=7 fix
hasAttr = function(el, attrName){ //IE does not support Object.Prototype
return el.getAttribute(attrName) !== null;
};
} else {
hasAttr = function(el, attrName){
return el.hasAttribute(attrName);
};
}
/*============================
CHECK IF DISPLAY IS RETINA
=============================*/
var retina = window.devicePixelRatio ? window.devicePixelRatio >= 1.2 ? 1 : 0 : 0;
/*============================
LOOP ALL IMAGES
=============================*/
for (var i = 0; i < images.length; i++) {
var image = images[i];
//set attr names
var srcAttr = ( retina && hasAttr(image, 'data-src2x') ) ? 'data-src2x' : 'data-src';
var baseAttr = ( retina && hasAttr(image, 'data-src-base2x') ) ? 'data-src-base2x' : 'data-src-base';
//check image attributes
if( !hasAttr(image, srcAttr) ){
continue;
}
var basePath = hasAttr(image, baseAttr) ? image.getAttribute(baseAttr) : '';
//get attributes
var queries = image.getAttribute(srcAttr);
//split defined query list
var queries_array = queries.split(',');
//loop queries
for(var j = 0; j < queries_array.length; j++){
var query = queries_array[j].replace(':','||').split('||');
var condition = query[0];
var response = query[1].replace(/(\[|\])/,"");
var conditionpx;
var bool;
//check if condition is below
if(condition.indexOf('<') !== -1){
conditionpx = condition.split('<');
if(queries_array[(j -1)]){
var prev_query = queries_array[(j - 1)].split(/:(.+)/);
var prev_cond = prev_query[0].split('<');
bool = (viewport <= conditionpx[1] && viewport > prev_cond[1]);
} else {
bool = (viewport <= conditionpx[1]);
}
} else {
conditionpx = condition.split('>');
if(queries_array[(j +1)]){
var next_query = queries_array[(j +1)].split(/:(.+)/);
var next_cond = next_query[0].split('>');
bool = (viewport >= conditionpx[1] && viewport < next_cond[1]);
} else {
bool = (viewport >= conditionpx[1]);
}
}
//check if document.width meets condition
if(bool){
var isCrossDomain = response.indexOf('//') !== -1 ? 1 : 0;
var new_source;
if(isCrossDomain === 1){
new_source = response;
} else {
new_source = basePath + response;
}
if(image.src !== new_source){
//change img src to basePath + response
image.setAttribute('src', new_source);
}
//break loop
break;
}
}
}
}
var resizeTimeout;
function resizeThrottler() {
// ignore resize events as long as an makeImagesResponsive execution is in the queue
if ( !resizeTimeout ) {
resizeTimeout = setTimeout(function() {
resizeTimeout = null;
makeImagesResponsive();
// The makeImagesResponsive will execute at a rate of 15fps
}, 66);
}
}
function listen(evnt, elem, func) {
if (elem.addEventListener) // W3C DOM
elem.addEventListener(evnt,func,false);
else if (elem.attachEvent) { // IE DOM
return elem.attachEvent("on"+evnt, func);
}
}
if(window.addEventListener){
listen('DOMContentLoaded', window ,function(){
makeImagesResponsive();
});
window.addEventListener('resize', resizeThrottler,false);
} else {
listen('load',window,function(){
makeImagesResponsive();
});
}