I am trying to do some calculation with the height of an element and i need to factor in the padding of an element.
var element_top = $(".box").position().top;
var element_height = $(".box").height();
var element_padding = $(".box").css("padding");
the first two give me numbers i can user for addition and subtraction but the the padding gives me 8px...any ideas...I am trying to u开发者_运维百科se the padding to subract from the height to get the actual height from the box element
You could use the following code but not shure if it's the best practice to go with .
var element_padding = parseInt($(".box").css("padding"));
You could just use outerHeight()
and skip the need for performing the calculations yourself:
var height = $('.box').outerHeight(true);
//the true argument includes margins in the calculation
For individual values (padding, margin, etc.) the parseInt
approach is what I often use, as has been suggested already.
You can just delete the px from the element-padding like this:
element-padding.replace("px", "");
Hope it helps..
You won't get the actual padding value unless you request its orientation specifically.
You should go with:
var element_padding = parseInt($(".box").css("padding-top"), 10);
精彩评论