2009
Jul
05




Scripting Computed Styles

The W3C standard API for determining the computed style of an element is the getComputedStyle() method of the Window object. The first argument to this method is the element whose computed style is desired. The second argument is any CSS pseudoelement, such as ":before" or ":after" whose style is desired. In the Mozilla and Firefox implementation of this method, the second argument is required and may not be omitted. As a result, you'll usually see getComputedStyle() invoked with null as its second argument.

IE provides a simpler alternative: every HTML element has a currentStyle property that holds its computed style.

Example
  1. var CSS = {
  2. getStyle: function(element) {
  3. if (!element) {
  4. return;
  5. }
  6. if (typeof element === "string") {
  7. element = document.getElementById(element);
  8. }
  9. if (element.currentStyle) { // For IE
  10. return element.currentStyle;
  11. } else if (document.defaultView && document.defaultView.getComputedStyle) { // For Modern Browsers
  12. return document.defaultView.getComputedStyle(element, null);
  13. } else {
  14. return element.style;
  15. }
  16. }
  17. };

Retrive Global/External CSS Properties

These two approaches frequently require different ways of referring to the style properties. In the case of the IE currentStyle object, references are made via the same object model syntax as is used for getting and setting style values. Therefore, hyphenated CSS property names must be referenced via the intercapitalization system (e.g., margin-left becomes marginLeft). But the property name for the W3C DOM getPropertyValue() method must be in the CSS property format (e.g., margin-left is margin-left).

Also be aware that for some CSS properties, different browser versions may return different value types-especially in colors that are specified by CSS syntax other than rgb(r, g, b). For example, if you set the color with a plain-language color name (e.g., orange), the value returned from the browsers may be in a different format. For the most part, if you specify colors in rgb(r, g, b) format, you'll get that back.

CSS values consisting of length measurements typically contain units (pixels, points, ems, and so on). If you tend to utilize the value of a style property for any math, such as adding five pixels to the left edge of a positioned element, be sure to extract the numeric portion of sorting values that include units. Use the parseInt() function for integers and the parseFloat() function for numeric values that may have digits to the right of the decimal (e.g., 0.5em).

Once you assign a value to a property of an element's style object, the value can be read subsequently through the style property. But for consistency's sake, you can continue to read a value the getPropertyValue() method and currentStyle object because it returns the effective value applied to the element at any instant.

Example
  1. var CSS = {
  2. get: function(element, name) {
  3. if (!element) {
  4. return;
  5. }
  6. if (typeof element === "string") {
  7. element = document.getElementById(element);
  8. }
  9. // Convert "background-color" to "backgroundColor"
  10. var camelCase = name.replace(/\-(\w)/g, function(all, letter) {
  11. return letter.toUpperCase();
  12. });
  13. if (element.currentStyle) { // For IE
  14. return element.currentStyle[name] || element.currentStyle[camelCase];
  15. } else if (document.defaultView && document.defaultView.getComputedStyle) { // For Modern Browsers
  16. name = name.replace(/([A-Z])/g, "-$1").toLowerCase();
  17. return document.defaultView.getComputedStyle(element, null).getPropertyValue(name);
  18. } else {
  19. return element.style[name] || element.style[camelCase];
  20. }
  21. },
  22. set: function(element, properties) {
  23. if (!element) {
  24. return;
  25. }
  26. if (typeof element === "string") {
  27. element = document.getElementById(element);
  28. }
  29. for (var name in properties) {
  30. // Convert "background-color" to "backgroundColor"
  31. var camelCase = name.replace(/\-(\w)/g, function(all, letter) {
  32. return letter.toUpperCase();
  33. });
  34. if (typeof properties[name] === "number") {
  35. element.style[camelCase] = (parseInt(properties[name], 10) || 0) + "px";
  36. }
  37. if (typeof properties[name] === "string") {
  38. element.style[camelCase] = properties[name];
  39. }
  40. }
  41. }
  42. };

View Demo

Related Posts


回應 (Leave a comment)