-
Notifications
You must be signed in to change notification settings - Fork 0
/
imaginari.js
50 lines (40 loc) · 1.24 KB
/
imaginari.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
var Imaginari = (function (){
var breakpoints = [2400, 1800, 1600, 1400, 980, 768, 480]; // High to low
var currentBreakpoint = -1;
var screenWidth;
var pixelRatio = ( 'devicePixelRatio' in window ? ',' + devicePixelRatio : ',1' );
init = function () {
this.binds();
this.monitor(); // Initial page load
},
binds = function() {
window.addEventListener( 'resize', this.monitor, false );
},
monitor = function() {
this.getScreenWidth();
if ( this.screenWidth !== this.currentBreakpoint ) {
this.currentBreakpoint = this.screenWidth;
this.setCookie();
this.updateImages();
}
},
updateImages = function() {
var images = document.images;
for ( var i = 0; i < images.length; i++ ) {
if ( images[i].src.indexOf( 'assets' ) === -1 ) {
images[i].src = images[i].src.substring( 0, ( images[i].src.indexOf('?') > 0 ? images[i].src.indexOf( '?' ) : images[i].src.length ) ) + '?' + this.screenWidth;
}
}
},
getScreenWidth = function() {
for ( var i = 0; i < breakpoints.length; i++ ) {
if ( window.innerWidth < breakpoints[i] ) {
this.screenWidth = breakpoints[i];
}
}
},
setCookie = function() {
document.cookie = 'resolution=' + this.screenWidth + pixelRatio +'; path=/';
};
return this.init();
}());