2012年11月6日 星期二

Get DIV Height

1 Problem:
   Sometimes you should make the layout in html using the css,
   and first should get the height from two div,
   the often use js code offsetHeight property,
   but it can't the real the height??


2. Reason:
    Because the offsetHeight didn't contain the margin height
     [offsetHeight property useage]
  Returns the height of the visible area for an object, in pixels. The value   contains the height with the padding, scrollBar, and the border, but does not include the margin.


3. solution:
    add the maring, but you should reference how to get margin top value

/* Get the div offsetHeight value*/
function getDivHeight(divIdName) {
    var div = document.getElementById(divIdName);
    var divHeight = 0;
   
    if (div){
        divHeight =  div.offsetHeight;
    }
     
    return divHeight;
}

/* I didn't know now to get the css top margin */
function getMainTopMarginHeight() {
    return 70;  /* Fix the content values*/
}


var mainBlockHeightTopMargin =  getDivHeight("main") +  getMainTopMarginHeight();


Reference





沒有留言:

張貼留言