I want to get the sum of margin-top
size and height
of another layer.
the margin-top of .nav1
is 30px and the height of .main
is 28px
I use this code:
$('.nav').css('margin-top').replace('px', '') + $('.main').outerHeight()
but the result of my sum is 3028
How can I calculate the sum of these two numbers?
You can use outerHeight(true)
to add margins to height without the maths, if you want to count margin-top
and margin-bottom
properties : http://api.jquery.com/outerheight/#outerHeight-includeMargin
var heightWithMargin = $('.main').outerHeight(true);
This is because you are appending two strings, instead of adding two integers. Try this:
var total = parseInt($('.nav').css('margin-top'), 10) + $('.main').outerHeight();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With