1

jQuery(".container").css("margin-right")给我:auto,而我想要类似52pxor的东西52

4

3 回答 3

3

您可以得到父宽度减去当前块的宽度并除以 2

于 2013-10-22T12:54:43.160 回答
2

This method calculates the remaining space between an elements width and the remaining width in the container, which is what margin: auto SHOULD do. This does NOT necessarily return the margin that the element has.

var parentwidth = $('.container').parent().width();
var childwidth = $('.container').width();
var marginLeft = (parentwidth-childwidth)/2;

NB: if there is any padding this must be compensated for.

于 2013-10-22T12:58:20.457 回答
0

您可以使用$(".container").offset()获取左侧位置。因此,当前控件减去(-)当前控件的左侧位置为您提供了必要的输出。

于 2013-10-22T13:05:29.080 回答