Q:

Why margin can't be add to height offset as digit?

I used code as below to get marginTop, and I found it is "8px", then I replace "px" to "", it show "8", but when I add it to height offset, it never worked!
does anyone tell me why?
var computedStyle = window.getComputedStyle ? getComputedStyle(document.body, null) : document.body.currentStyle;
var marginTop = computedStyle['marginTop'].replace('px', '');
alert(marginTop);
window.scroll(0, elem.offsetTop - headerHeight + marginTop + marginTop);

A:

var marginTop = computedStyle['marginTop'].replace('px', '');

the marginTop contains string value and you are doing this with a string:
elem.offsetTop - headerHeight + marginTop + marginTop

You need to convert the string to integer using parseInt().

