Requires

Provides

Contains methods for interacting with the styles of Elements in a fashionable way.

License:
MIT-style license.
  1. 18
  2. 19
  3. 20
  4. 21
  5. 22
  6. 23
  7. 24
  8. 25
  9. 26
  10. 27
  11. 28
  12. 29
  13. 30
  14. 31
  15. 32
  16. 33
  17. 34
  18. 35
  19. 36
  20. 37
  21. 38
  22. 39
  23. 40
  24. 41
  25. 42
  26. 43
  27. 44
  28. 45
  29. 46
  30. 47
  31. 48
  32. 49
  33. 50
  34. 51
  35. 52
  36. 53
  37. 54
  38. 55
  39. 56
  40. 57
  41. 58
  42. 59
  43. 60
  44. 61
  45. 62
  46. 63
  47. 64
  48. 65
  49. 66
  50. 67
  51. 68
  52. 69
  53. 70
  54. 71
  55. 72
  56. 73
  57. 74
  58. 75
  59. 76
  60. 77
  61. 78
  62. 79
  63. 80
  64. 81
  65. 82
  66. 83
  67. 84
  68. 85
  69. 86
  70. 87
  71. 88
  72. 89
  73. 90
  74. 91
  75. 92
  76. 93
  77. 94
  78. 95
  79. 96
  80. 97
  81. 98
  82. 99
  83. 100
  84. 101
  85. 102
  86. 103
  87. 104
  88. 105
  89. 106
  90. 107
  91. 108
  92. 109
  93. 110
  94. 111
  95. 112
  96. 113
  97. 114
  98. 115
  99. 116
  100. 117
  101. 118
  102. 119
  103. 120
  104. 121
  105. 122
  106. 123
  107. 124
  108. 125
  109. 126
  110. 127
  111. 128
  112. 129
  113. 130
  114. 131
  115. 132
  116. 133
  117. 134
  118. 135
  119. 136
  120. 137
  121. 138
  122. 139
  123. 140
  124. 141
  125. 142
  126. 143
  127. 144
  128. 145
  129. 146
  130. 147
  131. 148
  132. 149
  133. 150
  134. 151
  135. 152
  136. 153
  137. 154
  138. 155
  139. 156
  140. 157
  141. 158
(function(){ var html = document.html; Element.Properties.styles = {set: function(styles){ this.setStyles(styles); }}; var hasOpacity = (html.style.opacity != null); var reAlpha = /alpha\(opacity=([\d.]+)\)/i; var setOpacity = function(element, opacity){ if (!element.currentStyle || !element.currentStyle.hasLayout) element.style.zoom = 1; if (hasOpacity){ element.style.opacity = opacity; } else { opacity = (opacity == 1) ? '' : 'alpha(opacity=' + opacity * 100 + ')'; var filter = element.style.filter || element.getComputedStyle('filter') || ''; element.style.filter = reAlpha.test(filter) ? filter.replace(reAlpha, opacity) : filter + opacity; } }; Element.Properties.opacity = { set: function(opacity){ var visibility = this.style.visibility; if (opacity == 0 && visibility != 'hidden') this.style.visibility = 'hidden'; else if (opacity != 0 && visibility != 'visible') this.style.visibility = 'visible'; setOpacity(this, opacity); }, get: (hasOpacity) ? function(){ var opacity = this.style.opacity || this.getComputedStyle('opacity'); return (opacity == '') ? 1 : opacity; } : function(){ var opacity, filter = (this.style.filter || this.getComputedStyle('filter')); if (filter) opacity = filter.match(reAlpha); return (opacity == null || filter == null) ? 1 : (opacity[1] / 100); } }; var floatName = (html.style.cssFloat == null) ? 'styleFloat' : 'cssFloat'; Element.implement({ getComputedStyle: function(property){ if (this.currentStyle) return this.currentStyle[property.camelCase()]; var defaultView = Element.getDocument(this).defaultView, computed = defaultView ? defaultView.getComputedStyle(this, null) : null; return (computed) ? computed.getPropertyValue((property == floatName) ? 'float' : property.hyphenate()) : null; }, setOpacity: function(value){ setOpacity(this, value); return this; }, getOpacity: function(){ return this.get('opacity'); }, setStyle: function(property, value){ switch (property){ case 'opacity': return this.set('opacity', parseFloat(value)); case 'float': property = floatName; } property = property.camelCase(); if (typeOf(value) != 'string'){ var map = (Element.Styles[property] || '@').split(' '); value = Array.from(value).map(function(val, i){ if (!map[i]) return ''; return (typeOf(val) == 'number') ? map[i].replace('@', Math.round(val)) : val; }).join(' '); } else if (value == String(Number(value))){ value = Math.round(value); } this.style[property] = value; return this; }, getStyle: function(property){ switch (property){ case 'opacity': return this.get('opacity'); case 'float': property = floatName; } property = property.camelCase(); var result = this.style[property]; if (!result || property == 'zIndex'){ result = []; for (var style in Element.ShortStyles){ if (property != style) continue; for (var s in Element.ShortStyles[style]) result.push(this.getStyle(s)); return result.join(' '); } result = this.getComputedStyle(property); } if (result){ result = String(result); var color = result.match(/rgba?\([\d\s,]+\)/); if (color) result = result.replace(color[0], color[0].rgbToHex()); } if (Browser.opera || (Browser.ie && isNaN(parseFloat(result)))){ if ((/^(height|width)$/).test(property)){ var values = (property == 'width') ? ['left', 'right'] : ['top', 'bottom'], size = 0; values.each(function(value){ size += this.getStyle('border-' + value + '-width').toInt() + this.getStyle('padding-' + value).toInt(); }, this); return this['offset' + property.capitalize()] - size + 'px'; } if (Browser.opera && String(result).indexOf('px') != -1) return result; if ((/^border(.+)Width|margin|padding/).test(property)) return '0px'; } return result; }, setStyles: function(styles){ for (var style in styles) this.setStyle(style, styles[style]); return this; }, getStyles: function(){ var result = {}; Array.flatten(arguments).each(function(key){ result[key] = this.getStyle(key); }, this); return result; } }); Element.Styles = { left: '@px', top: '@px', bottom: '@px', right: '@px', width: '@px', height: '@px', maxWidth: '@px', maxHeight: '@px', minWidth: '@px', minHeight: '@px', backgroundColor: 'rgb(@, @, @)', backgroundPosition: '@px @px', color: 'rgb(@, @, @)', fontSize: '@px', letterSpacing: '@px', lineHeight: '@px', clip: 'rect(@px @px @px @px)', margin: '@px @px @px @px', padding: '@px @px @px @px', border: '@px @ rgb(@, @, @) @px @ rgb(@, @, @) @px @ rgb(@, @, @)', borderWidth: '@px @px @px @px', borderStyle: '@ @ @ @', borderColor: 'rgb(@, @, @) rgb(@, @, @) rgb(@, @, @) rgb(@, @, @)', zIndex: '@', 'zoom': '@', fontWeight: '@', textIndent: '@px', opacity: '@' };

<1.2compat>

  1. 162
Element.Styles = new Hash(Element.Styles);

</1.2compat>

  1. 166
  2. 167
  3. 168
  4. 169
  5. 170
  6. 171
  7. 172
  8. 173
  9. 174
  10. 175
  11. 176
  12. 177
  13. 178
  14. 179
  15. 180
  16. 181
  17. 182
  18. 183
  19. 184
Element.ShortStyles = {margin: {}, padding: {}, border: {}, borderWidth: {}, borderStyle: {}, borderColor: {}}; ['Top', 'Right', 'Bottom', 'Left'].each(function(direction){ var Short = Element.ShortStyles; var All = Element.Styles; ['margin', 'padding'].each(function(style){ var sd = style + direction; Short[style][sd] = All[sd] = '@px'; }); var bd = 'border' + direction; Short.border[bd] = All[bd] = '@px @ rgb(@, @, @)'; var bdw = bd + 'Width', bds = bd + 'Style', bdc = bd + 'Color'; Short[bd] = {}; Short.borderWidth[bdw] = Short[bd][bdw] = All[bdw] = '@px'; Short.borderStyle[bds] = Short[bd][bds] = All[bds] = '@'; Short.borderColor[bdc] = Short[bd][bdc] = All[bdc] = 'rgb(@, @, @)'; }); })();