forked from bitovi/jquerypp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstyles.js
More file actions
90 lines (78 loc) · 2.34 KB
/
styles.js
File metadata and controls
90 lines (78 loc) · 2.34 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
steal('jquery/dom').then(function( $ ) {
var getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,
rupper = /([A-Z])/g,
rdashAlpha = /-([a-z])/ig,
fcamelCase = function( all, letter ) {
return letter.toUpperCase();
},
getStyle = function( elem ) {
if ( getComputedStyle ) {
return getComputedStyle(elem, null);
}
else if ( elem.currentStyle ) {
return elem.currentStyle;
}
},
rfloat = /float/i,
rnumpx = /^-?\d+(?:px)?$/i,
rnum = /^-?\d/;
$.styles = function( el, styles ) {
if (!el ) {
return null;
}
var currentS = getStyle(el),
oldName, val, style = el.style,
results = {},
i = 0,
left, rsLeft, camelCase, name;
for (; i < styles.length; i++ ) {
name = styles[i];
oldName = name.replace(rdashAlpha, fcamelCase);
if ( rfloat.test(name) ) {
name = jQuery.support.cssFloat ? "float" : "styleFloat";
oldName = "cssFloat";
}
if ( getComputedStyle ) {
name = name.replace(rupper, "-$1").toLowerCase();
val = currentS.getPropertyValue(name);
if ( name === "opacity" && val === "" ) {
val = "1";
}
results[oldName] = val;
} else {
camelCase = name.replace(rdashAlpha, fcamelCase);
results[oldName] = currentS[name] || currentS[camelCase];
if (!rnumpx.test(results[oldName]) && rnum.test(results[oldName]) ) { //convert to px
// Remember the original values
left = style.left;
rsLeft = el.runtimeStyle.left;
// Put in the new values to get a computed value out
el.runtimeStyle.left = el.currentStyle.left;
style.left = camelCase === "fontSize" ? "1em" : (results[oldName] || 0);
results[oldName] = style.pixelLeft + "px";
// Revert the changed values
style.left = left;
el.runtimeStyle.left = rsLeft;
}
}
}
return results;
};
/**
* @function jQuery.fn.styles
* @parent jQuery.styles
* @plugin jQuery.styles
*
* Returns a set of computed styles. Pass the names of the styles you want to
* retrieve as arguments:
*
* $("div").styles('float','display')
* // -> { cssFloat: "left", display: "block" }
*
* @param {String} style pass the names of the styles to retrieve as the argument list
* @return {Object} an object of `style` : `value` pairs
*/
$.fn.styles = function() {
return $.styles(this[0], $.makeArray(arguments));
};
});