2011-07-09 66 views
0

我想检索身体的scrollHeight为了重置ID =“background1”的div的大小。如果在我的外部CSS中,我将该ID设置为position:absolute,scrollHeight总是返回0.如果将其设置为position:relative scrollHeight给了我正确的值。当然,CSS中有更多的选择器,其中一个被设置为绝对位置:保持原样不会改变上述行为。代码:scrollHeight不与位置:绝对

<script type="text/javascript"> 

    function getElement(elem) { 

    var art; 

    if (document.all) {     // this is the way old msie versions work  
     art = document.all[elem]; 
    } 
    else if (document.layers) {   // this is the way nn4 works  
     art = document.layers[elem]; 
    } 
    else if (document.getElementById) { // this is the way the standards work  
     art = document.getElementById(elem); 
    } 

    return art; 
} 

function toggleArticleDisplay(elem) { 

    var artArray = document.getElementsByTagName("article"); 

    for(var i = 0; i < artArray.length; i++) { 
    artArray[i].style.display = "none"; 
    } 

    var art = getElement(elem); 
    art.style.display = "block"; 

    var bg1 = document.getElementById("background1"); 
    bg1.style.height = "550px"; 

    var b = document.getElementById("potoococha"); 

    alert(b.scrollHeight); 

    bg1.style.height = (b.scrollHeight >= window.innerHeight) ? 
      (b.scrollHeight+20) + "px" : window.innerHeight + "px"; 
} 

</script> 

</head> 

<body id="potoococha" onLoad="toggleArticleDisplay('homeText')"> 

<div id="background1" style="height:615px;"/> 

而现在的CSS:

div#background1 { position   : absolute; 
        left    : 45px; 
        top    : 0px; 
        z-index   : 1; 
        width   : 50px; 
        background-color : rgba(182,181,91,1.0); } 

仅仅改变那个位置相对使javascript函数返回警报适当scrollHeight属性,但我需要这个因为其他原因而绝对的。如果某些元素被绝对定位,scrollHeight是否不起作用?正如我所说,还有另一个绝对定位的元素,它对scrollHeight的返回值没有任何影响。只有身体元素的第一个孩子才是这个元素似乎是一个问题。有任何想法吗?

回答

1

这是一个有点脏的黑客攻击,但你可以尝试这样做:如果

bg1.style.position = "relative"; 

var savedHeight = b.scrollHeight; 

bg1.style.position = "absolute"; 

不知道这会工作,但也许试试看。