-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.px2percent.js
51 lines (45 loc) · 1.38 KB
/
jquery.px2percent.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
/*! JQuery plugin that let you switch from absolute position
* and size (top and left) expressed in px to percent (%).
*
* By Pierre Ruyssen <[email protected]>.
*
* License: AGPL V3.0.
* http://www.fsf.org/licensing/licenses/agpl-3.0.html
*/
(function($){
$.fn.px2percent = function(ref_width, ref_height, zoom_level) {
/* Switch width, height, top and left css properties
* of objects to percent (%), if not already.
* To do so, it uses the given ref_width and ref_height
* variables (and not the actual sizes of the container).
*
* If given, zoom level (in %) will be applied to the ref sizes
* of the container.
*/
var x,
y,
position,
element = $(this);
if(zoom_level) {
zoom_level /= 100;
ref_width *= zoom_level;
ref_height *= zoom_level;
}
if(element.css("width").indexOf("px") > -1) {
// The element size is expressed in px
element.css({
width: element.width() / ref_width * 100 + "%",
height: element.height() / ref_height * 100 + "%"
});
}
if(element.css("top").indexOf("px") > -1) {
//elements with position in px
position = element.position();
element.css({
left: position.left / ref_width * 100 + "%",
top: position.top / ref_height * 100 + "%"
});
}
return element;
};
})(jQuery);