-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.resolution.js
138 lines (117 loc) · 3.18 KB
/
jquery.resolution.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
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
135
136
137
138
/*
* Part of Nitriques Solutions inc. (http://www.nitriques.com) jQuery plug-in bundle
*
* Use this plugin to automatically switch css class on root element in order to accomodate differents
* screen resolution.
*
* Usage $.ntr$fontSize(opts);
*
* options w and h always means that the css class will be applied if the resolution is less than mention
*
* Examples:
*
* 1- Default: **** resulting css will be 'resolution-w1024'
* $.ntr$resolution({
* res: [
* {w:1024}
* ]
* });
*
* 2- With options:
* $.ntr$resolution({
* res: [
* {w:1024}
* ],
* prefix: 'my-resolution', // css prefix class
* root: '#my-root' // root DOM element that gets the css class
* });
*
* 4- With width and height resolution options: **** resulting css will be 'resolution-w1024-h720'
* $.ntr$resolution({
* res: [
* {w:1024, h:720}
* ]
* });
*
* 4- With multiple resolution options: ** NOTE they must be in ASC order
* $.ntr$resolution({
* res: [
* {w:1024, h:720}, //**** resulting css will be 'resolution-w1024-h720'
* {w:1024}, //**** resulting css will be 'resolution-w1024'
* {w:1200}, //**** resulting css will be 'resolution-w1200'
* {h:900}, //**** resulting css will be 'resolution-h900'
* ]
* });
*
* Liscence under the The Code Project Open License (CPOL)
* http://www.codeproject.com/info/cpol10.aspx
* Name: jquery.resolution.js
* Date: 2011-06-16
* Version: 1.0
* Pre-requisites: none;
*
* Version 1.0
* Initial version
*/
(function ($, undefined) {
var defaults = {
res: [],
root: 'body',
prefix: 'resolution'
};
$.extend({ // jQuery plugin
ntr$resolution: function(options) {
var opts = $.extend({}, defaults, options),
win = $(window);
function getClass(w, h) {
var c = opts.prefix + '-';
if (w && h) {
c += 'w' + w + '-h' + h;
} else if (w) {
c += 'w' + w;
} else if (h) {
c += 'h' + h;
}
return c;
}
function process() {
var x = 0,
ww = win.width(),
wh = win.height(),
found = false,
res = null;
// remove all resolution classes
// from : http://stackoverflow.com/questions/2644299/jquery-removeclass-wildcard
$(opts.root).attr('class',
function(i, c){
return c.replace(new RegExp('[ ]?'+opts.prefix+'-[^ ]+','g'), '');
}
);
// try to match a resolution
while (!found && x < opts.res.length) {
// update pointer
res = opts.res[x];
if (res.w && res.h) {
found = ww < res.w && wh < res.h;
} else if (res.w) {
found = ww < res.w;
} else if (res.h) {
found = wh < res.h;
}
// increment
x++;
}
// set class if found
if (found && res) {
$(opts.root).addClass(getClass(res.w, res.h));
}
return false;
}
// process once to detect the current resolution
process();
// hook events
win.resize(process);
return $;
}
}); // extend
})(jQuery);