Jump To …

styles.js

(function ($) {
  var getComputedStyle = document.defaultView && document.defaultView.getComputedStyle,

The following variables are used to convert camelcased attribute names into dashed names, e.g. borderWidth to border-width

    rupper = /([A-Z])/g,
    rdashAlpha = /-([a-z])/ig,
    fcamelCase = function (all, letter) {
      return letter.toUpperCase();
    },

Returns the computed style for an elementn

    getStyle = function (elem) {
      if (getComputedStyle) {
        return getComputedStyle(elem, null);
      }
      else if (elem.currentStyle) {
        return elem.currentStyle;
      }
    },

Checks for float px and numeric values

    rfloat = /float/i,
    rnumpx = /^-?\d+(?:px)?$/i,
    rnum = /^-?\d/;

Returns a list of styles for a given element

  $.styles = function (el, styles) {
    if (!el) {
      return null;
    }
    var currentS = getStyle(el),
      oldName, val, style = el.style,
      results = {},
      i = 0,
      left, rsLeft, camelCase, name;

Go through each style

    for (; i < styles.length; i++) {
      name = styles[i];
      oldName = name.replace(rdashAlpha, fcamelCase);

      if (rfloat.test(name)) {
        name = $.support.cssFloat ? "float" : "styleFloat";
        oldName = "cssFloat";
      }

If we have getComputedStyle available

      if (getComputedStyle) {

convert camelcased property names to dashed name

        name = name.replace(rupper, "-$1").toLowerCase();

use getPropertyValue of the current style object

        val = currentS.getPropertyValue(name);

default opacity is 1

        if (name === "opacity" && val === "") {
          val = "1";
        }
        results[oldName] = val;
      } else {

Without getComputedStyles

        camelCase = name.replace(rdashAlpha, fcamelCase);
        results[oldName] = currentS[name] || currentS[camelCase];

convert to px

        if (!rnumpx.test(results[oldName]) && rnum.test(results[oldName])) {

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;
  };


  $.fn.styles = function () {

Pass the arguments as an array to $.styles

    return $.styles(this[0], $.makeArray(arguments));
  };

  return $;
})(jQuery);