te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - what is the jQuery outerHeight() equivalent in YUI - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - what is the jQuery outerHeight() equivalent in YUI - Stack Overflow

programmeradmin3浏览0评论

In jQuery, I can very easily get the current puted 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.

In jQuery, I can very easily get the current puted 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.

Share Improve this question edited May 23, 2017 at 12:31 CommunityBot 11 silver badge asked Oct 1, 2012 at 20:32 HristoHristo 46.5k67 gold badges168 silver badges234 bronze badges 5
  • Have you tried .get("offsetHeight")? – VisioN Commented Oct 1, 2012 at 20:37
  • 1 I believe offsetHeight does not include the margin values, which are important to me – Hristo Commented Oct 1, 2012 at 20:39
  • stackoverflow./questions/3039828/… – vol7ron Commented Oct 1, 2012 at 20:55
  • 1 just a heads up, if your margin is in %, the outerHeight value in safari is incorrect for jquery – Huangism Commented Oct 1, 2012 at 21:00
  • thanks for the heads up. it should always be in pixels, but i'll make sure – Hristo Commented Oct 1, 2012 at 21:01
Add a ment  | 

4 Answers 4

Reset to default 5

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./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./jquery/jquery/blob/master/src/dimensions.js).

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

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

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

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

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!

发布评论

评论列表(0)

  1. 暂无评论