在计算元素的offset的时候,如果其多个父级定位有相对定位,则该元素的offset的值会受相对定位的影响。
比如想算div.container 的offset,则会有60像素的偏移。
在计算元素的offset的时候,如果其多个父级定位有相对定位,则该元素的offset的值会受相对定位的影响。
比如想算div.container 的offset,则会有60像素的偏移。
不会呀,估计你代码有其他问题
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<style>
body{margin:0;padding:0;position:relative;}
#box{position:relative;width:200px;height:200px;background:#ddd;margin:25px auto;}
#test{width:100px;height:100px;background:#000;margin:0 auto;}
.big{position:relative;width:250px;height:250px;background:#dd4215;margin:20px auto;overflow:hidden;}
</style>
<div class="big">
<div id="box">
<div id="test"></div>
</div>
</div>
<button>点我看offsetTop</button>
<script>
var oTest = document.getElementById('test');
var oButton = document.getElementsByTagName('button')[0];
oButton.onclick = function () {
alert('top:' + oTest.offsetTop + '\r\nleft:' + oTest.offsetLeft)
};
</script>
</body>
</html>
10 回答11.1k 阅读
6 回答3k 阅读
5 回答4.8k 阅读✓ 已解决
4 回答3.1k 阅读✓ 已解决
2 回答2.7k 阅读✓ 已解决
3 回答5.1k 阅读✓ 已解决
5 回答2k 阅读
Element.getBoundingClientRect().left