Hristo
Hristo

Reputation: 46497

what is the jQuery outerHeight() equivalent in YUI

In jQuery, I can very easily get the current computed height for an element that includes padding, border, and optionally margin by using outerHeight()...

// returns height of element + border + padding + margin
$('.my-element').outerHeight(true);

How would I do this in YUI? I'm currently using version 2.8.1.

Similar to this question, I can always do getComputedStyle() for height, border, padding, and margin, but that is a lot of manual labor which includes parsing the return values and grabbing the correct values that are needed and doing the math myself.

Isn't there some equivalent function to jQuery's outerHeight() in YUI that does all of this for me?

Solution

I ended up writing my own solution since I couldn't find a jQuery outerheight() equivalent. I've posted the solution as an answer here.

Upvotes: 10

Views: 6400

Answers (4)

Hristo
Hristo

Reputation: 46497

I ended up writing my own little utility function for this:

/**
 * Calculates the outer height for the given DOM element, including the 
 * contributions of padding, border, and margin.
 * 
 * @param el - the element of which to calculate the outer height
 */
function calculateElementOuterHeight(el) {

  var height = 0;
  var attributeHeight = 0;
  var attributes = [
      'height', 
      'border-top-width', 
      'border-bottom-width', 
      'padding-top', 
      'padding-bottom', 
      'margin-top', 
      'margin-bottom'
  ];

  for (var i = 0; i < attributes.length; i++) {

    // for most browsers, getStyle() will get us a value for the attribute 
    // that is parse-able into a number
    attributeHeight = parseInt(YAHOO.util.Dom.getStyle(el, attributes[i]), 10);

    // if the browser returns something that is not parse-able, like "auto", 
    // try getComputedStyle(); should get us what we need
    if (isNaN(attributeHeight)) {
      attributeHeight = parseInt(YAHOO.util.Dom.getComputedStyle(el, attributes[i]), 10);
    }

    // if we have an actual numeric value now, add it to the height, 
    // otherwise ignore it
    if (!isNaN(attributeHeight)) {
      height += attributeHeight;
    }
  }

  return isNaN(height) ? 0 : height;
}

This seems to work across all modern browsers. I've tested it in Chrome, Firefox (idk about 3.6, but the latest version works), Safari, Opera, & IE 7,8,9. Let me know what you guys think!

Upvotes: 1

juandopazo
juandopazo

Reputation: 6329

There is no built-in way of getting the outer width of an element with its margin in YUI. Like @jshirley mentions, there is offsetWidth, but it doesn't take margins into account. You can however create a function that adds the margin very easily:

Y.Node.ATTRS.outerHeight = {
  getter: function () {
    return this.get('offsetHeight') + 
           parseFloat(this.getComputedStyle('marginTop')) + 
           parseFloat(this.getComputedStyle('marginBottom'));
  }
};

Y.Node.ATTRS.outerWidth = {
  getter: function () {
    return this.get('offsetWidth') +
           parseFloat(this.getComputedStyle('marginLeft')) +
           parseFloat(this.getComputedStyle('marginRight'));
  }
};

Then you can get the outer width by doing Y.one(selector).get('outerWidth'). Here's an example based on @jshirley's code: http://jsbin.com/aretab/4/.

Just keep in mind that dimensions are usually a source of bugs in browsers and this doesn't take into account some stuff (ie: dimensions of the document) jQuery tries to catch (see https://github.com/jquery/jquery/blob/master/src/dimensions.js).

Upvotes: 5

vol7ron
vol7ron

Reputation: 42109

If you wanted to avoid the manual labor, wrap the element in a div and get the computed style of that.

If it's something you're doing more than once, create a function/plugin to reuse.

Upvotes: 2

jshirley
jshirley

Reputation: 264

According to http://www.jsrosettastone.com/, you should be using .get('offsetHeight').

This example shows the equivalency: http://jsbin.com/aretab/1/edit

Upvotes: 1

Related Questions