Skip to content

Instantly share code, notes, and snippets.

@Andyliwr
Created May 3, 2016 09:06
Show Gist options
  • Save Andyliwr/f2f71d1c8a7ad9789f2e70e79c701ad7 to your computer and use it in GitHub Desktop.
Save Andyliwr/f2f71d1c8a7ad9789f2e70e79c701ad7 to your computer and use it in GitHub Desktop.
坐标计算
<div class="coordinate_out">
<div id="coordinate">内部</div>
</div>
//方法一
function offsetSum(node) {
var top=0, left=0
while(node) {
top = top + parseInt(node.offsetTop);
left = left + parseInt(node.offsetLeft);
node = node.offsetParent;
}
return {top: top, left: left}
}
//方法二
function offsetSum2(node) {
return node.getBoundingClientRect();
}
node = document.getElementById('coordinate');
rect = offsetSum(node);
rect2 = offsetSum2(node);
console.log(rect);
console.log(rect2);
.coordinate_out {
position: relative;
top: 100px;
left: 100px;
width: 500px;
}
#coordinate {
left: 100px;
top: 250px;
height: 30px;
background: #9ACD32;
position: absolute;
border: 2px solid red;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment