<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>javascript 封装函数,获得 高度及左边距</title>
<style>
#div1 { width:1000px; height:500px; background-color:#7A7A7A; marg in:0px auto;padding-top:200px;}
#div2 { width:800px; height:300px; background-color:#F54F52;margin:0px auto; padding-top:200px; }
#div3 { width:500px; height:100px; background-color:#3D9F48;margin:0px auto;}
</style>
<script>
window.onload = function(){
var oDiv3 = document.getElementById('div3');
var oDiv2 = document.getElementById('div2');
var oDiv1 = document.getElementById('div1');
/*
var iTop = 0;
var obj = oDiv3;
while(obj){
iTop += obj.offsetTop;
obj = obj.offsetParent;
}
alert(iTop);
*/
alert(getPos(oDiv3).top);
function getPos(obj){
var pos = {left:0,top:0};
while(obj){
pos.left += obj.offsetLeft;
pos.top += obj.offsetTop;
obj = obj.offsetParent;
}
return pos;
}
}
</script>
</head>
<body>
<div id="div1">
<div id="div2">
<div id="div3"></div>
</div>
</div>
</body>
</html>
javascript 封装函数,获得 高度及左边距
最新推荐文章于 2022-11-18 20:35:15 发布