<script>
var divBgTop=0;
function initDivTop()
{
divBgTop=document.getElementById("divBg").style.pixelTop;
alert(divBgTop);
}
</script>
<style>
.divBgCss
{
position:absolute;
left:100px;
top:100px;
width:100px;
height:100px;
background-color:red;
}
</style>
<body onload="initDivTop()">
<div class="divBgCss" id="divBg"></div>
</body>
the result is always 0. why?
What you’re after is the
offsetTop:Without specifying the unit, you will not get 0 but rather something else but not 100 – you better add unit like
pxso you won’t get wrong results when using the code.Live test case: http://jsfiddle.net/A9Mr2/1/