forked from 1Marc/hisrc
-
Notifications
You must be signed in to change notification settings - Fork 63
/
breakpoint.js
108 lines (92 loc) · 2.53 KB
/
breakpoint.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
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
/*
* Breakpoint jQuery Plugin
*
* Copyright (c) 2012
* Licensed under the MIT license.
*
*/
(function($){
$.breakpoint = {
els: $(),
init: false
};
$.fn.breakpoint = function() {
$.breakpoint.els = $.breakpoint.els.add(this);
if (!$.breakpoint.init) {
$(window).on('resize.breakpoint', function(){
$.breakpoint.els.trigger('swapres.breakpoint');
});
}
return this.each(function(){
var $el = $(this),
maxWidths = [],
minWidths = [];
$el.data('m1src', $el.attr('src'));
// parse breakpoints.
$.each( $el.data(), function(key, value) {
if (key && (key.toLowerCase().indexOf('maxwidth') === 0 || key.toLowerCase().indexOf('minwidth') === 0)) {
var width = key.substring(3).match( /\d+/g );
if (width.length === 1) {
var bp = {
key: key,
type: ( key.toLowerCase().indexOf('max') === 0 ? 'max' : 'min' ),
width: width[0]
};
if ( bp.type === 'max' ) {
maxWidths.push( bp );
}
else {
minWidths.push( bp )
}
}
}
});
console.log(minWidths);
// sort low to high.
minWidths.sort(function( a, b ) {
return a.width - b.width;
});
console.log(minWidths);
console.log(maxWidths);
// sort high to low.
maxWidths.sort(function( a, b ) {
return b.width - a.width;
});
console.log(maxWidths);
$el
.on('swapres.breakpoint', function(){
console.log('swapres');
// if breakpoints are defined just use them and ignore the rest.
if ( maxWidths.length > 0 || minWidths.length > 0 ) {
// loop threw each break point and try apply it.
var windowWidth = $(window).width();
var activeBreakpoint = null;
$.each( maxWidths, function(index, bp) {
if ( windowWidth <= bp.width ) {
console.log('matched breakpoint: ' + bp.key);
activeBreakpoint = bp;
}
});
$.each( minWidths, function(index, bp) {
if ( windowWidth >= bp.width ) {
console.log('matched breakpoint: ' + bp.key);
activeBreakpoint = bp;
}
});
// fallback to mobile first if no matches.
if ( activeBreakpoint != null ) {
console.log('using breakpoint: ' + activeBreakpoint.key);
$el.attr( 'src', $el.data( activeBreakpoint.key ) );
} else {
console.log('using mobile1st');
$el.attr( 'src', $el.data('m1src') );
}
} else {
console.log('using mobile1st');
$el.attr( 'src', $el.data('m1src') );
}
})
.trigger('swapres.breakpoint');
});
};
})(jQuery);