Created
May 3, 2016 09:06
-
-
Save Andyliwr/f2f71d1c8a7ad9789f2e70e79c701ad7 to your computer and use it in GitHub Desktop.
坐标计算
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="coordinate_out"> | |
<div id="coordinate">内部</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//方法一 | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.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