YUI - Get True element width?

拟墨画扇 提交于 2020-01-03 12:24:27

问题


I am using YUI and need to get the true width of the element. The width of an element can be determined as follows.

width + border-left + border-right + padding-left + padding-right + margin-left + margin-right.

Below is what I have come up with. It appears to be working. I was just wondering if this is the best way to go about determining this or is there there a more efficient way?

YUI().use('node', function(Y) {
    var node = Y.one('#nav');
    var nodeWidth = trueElementWidth(node);
    alert(nodeWidth);
});

function trueElementWidth(el) {
    var width = 0;
    var attributes = ['border-left', 'border-right', 'padding-left', 'padding-right', 'width', 'margin-right', 'margin-left'];
    for(var i=0; i < attributes.length; i++) {
        width = width + removePx(el.getComputedStyle(attributes[i]));
    }
    return width;
}

function removePx(el) {
    el = el.toString();
    length = el.length - 2;
    elDimension = parseInt(el.substring(0, length));
    return isNaN(elDimension) ? 0 : elDimension;
}

回答1:


There is an offsetWidth property that returns exactly what you want.



来源:https://stackoverflow.com/questions/4136616/yui-get-true-element-width

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!