jQuery有height() en width()函数,返回高度或宽度像素为整数…

我怎么能得到一个填充或边缘值的元素像素和整数使用jQuery?

我的第一个想法是:

var padding = parseInt(jQuery("myId").css("padding-top"));

但是,如果填充以ems为例,我如何才能得到以像素为单位的值?


查看由Chris Pebble建议的JSizes插件,我意识到我自己的版本是正确的:)。jQuery总是以像素为单位返回值,因此只需将其解析为整数就是解决方案。

感谢Chris Pebble和Ian Robinson


当前回答

请不要仅仅为了做一些本地已经可用的东西而去加载另一个库!

jQuery的.css()将%'s和em's转换为它们开始时的等效像素,parseInt()将从返回字符串的末尾删除'px'并将其转换为整数:

http://jsfiddle.net/BXnXJ/

$(document).ready(function () {
    var $h1 = $('h1');
    console.log($h1);
    $h1.after($('<div>Padding-top: ' + parseInt($h1.css('padding-top')) + '</div>'));
    $h1.after($('<div>Margin-top: ' + parseInt($h1.css('margin-top')) + '</div>'));
});

其他回答

下面是你如何获得周围的尺寸:

var elem = $('#myId');

var marginTopBottom  = elem.outerHeight(true) - elem.outerHeight();
var marginLeftRight  = elem.outerWidth(true)  - elem.outerWidth();

var borderTopBottom  = elem.outerHeight() - elem.innerHeight();
var borderLeftRight  = elem.outerWidth()  - elem.innerWidth();

var paddingTopBottom  = elem.innerHeight() - elem.height();
var paddingLeftRight  = elem.innerWidth()  - elem.width();

注意,每个变量,例如paddingTopBottom,包含元素两边边距的和;例如,paddingTopBottom == paddingTop + paddingBottom。我想知道有没有办法把它们分开。当然,如果它们相等,你可以除以2:)

你应该能够使用CSS (http://docs.jquery.com/CSS/css#name)。你可能需要更具体一些,比如"padding-left"或"margin-top"。

例子:

CSS

a, a:link, a:hover, a:visited, a:active {color:black;margin-top:10px;text-decoration: none;}

JS

$("a").css("margin-top");

结果是10px。

如果您想获取整数值,可以执行以下操作:

parseInt($("a").css("margin-top"))

不是死灵,但我做了这个,可以确定基于各种值的像素:

$.fn.extend({
  pixels: function (property, base) {
    var value = $(this).css(property);
    var original = value;
    var outer = property.indexOf('left') != -1 || property.indexOf('right') != -1 
      ? $(this).parent().outerWidth()
      : $(this).parent().outerHeight();

    // EM Conversion Factor
    base = base || 16;

    if (value == 'auto' || value == 'inherit') 
        return outer || 0;

    value = value.replace('rem', '');
    value = value.replace('em', '');

    if (value !== original) {
       value = parseFloat(value);
       return value ? base * value : 0;
    }

    value = value.replace('pt', '');

    if (value !== original) {
       value = parseFloat(value);
       return value ? value * 1.333333 : 0; // 1pt = 1.333px
    }

    value = value.replace('%', '');

    if (value !== original) {
      value = parseFloat(value);
      return value ? (outer * value / 100) : 0;
    }

    value = value.replace('px', '');
    return parseFloat(value) || 0;
  }
});

这样,我们会考虑到大小和自动/继承。

你也可以自己扩展jquery框架,比如:

jQuery.fn.margin = function() {
var marginTop = this.outerHeight(true) - this.outerHeight();
var marginLeft = this.outerWidth(true) - this.outerWidth();

return {
    top: marginTop,
    left: marginLeft
}};

因此,在jquery对象上添加一个名为margin()的函数,该函数返回一个类似offset函数的集合。

fx.

$("#myObject").margin().top

这个简单的函数可以做到:

$.fn.pixels = function(property) {
    return parseInt(this.css(property).slice(0,-2));
};

用法:

var padding = $('#myId').pixels('paddingTop');