如何使用javascript获取左上角的x和y坐标
我知道我们可以使用clientWidth和clientHeight获取宽度和高度,但是如何找出元素的左上角x和左上角y位置?
function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
while(1)
{
curleft += obj.offsetLeft;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.x)
curleft += obj.x;
return curleft;
}
function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
while(1)
{
curtop += obj.offsetTop;
if(!obj.offsetParent)
break;
obj = obj.offsetParent;
}
else if(obj.y)
curtop += obj.y;
return curtop;
}
检索HTML元素的位置(X,Y)
使用JavaScript查找HTML元素的X / Y
这两个链接应该是有帮助的。
使用jQuery,你可以通过调用.position()函数来完成。 喜欢:
$('#mydiv').position().left;
$('#mydiv').position().top;
这是最可靠的方法,因为它已经计算了检查元素及其父元素CSS的位置。
你可以在这里看到完整的实现:
第9077行的http://code.jquery.com/jquery-1.7.1.js
要获得顶部,您需要添加元素的offsetTop
和元素的offsetParent
的offsetTop
。 从DOM到document
。 这说明了绝对的,相对的和固定的定位。 为了得到左边,用offsetLeft
做同样的事情。
这两个函数将两个属性添加到Element
, documentOffsetTop
和documentOffsetLeft
,它们将得到任何元素的顶部/左侧:
window.Object.defineProperty( Element.prototype, 'documentOffsetTop', {
get: function () {
return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop : 0 );
}
} );
window.Object.defineProperty( Element.prototype, 'documentOffsetLeft', {
get: function () {
return this.offsetLeft + ( this.offsetParent ? this.offsetParent.documentOffsetLeft : 0 );
}
} );
这个演示展示了几种元素布局的组合,比较documentOffsetTop
和jQuery的offset().top
。
演示:http://jsfiddle.net/ThinkingStiff/3G7EZ/
脚本:
window.Object.defineProperty( Element.prototype, 'documentOffsetTop', {
get: function () {
return this.offsetTop + ( this.offsetParent ? this.offsetParent.documentOffsetTop : 0 );
}
} );
var line = document.getElementsByClassName( 'grid-line' )[0],
grid = document.getElementById( 'grid' );
for( var index = 2; index < 100; index++ ) {
var copy = line.cloneNode();
copy.textContent = ( index * 10 );
grid.appendChild( copy );
};
var offset = document.getElementById( 'absolute' );
offset.textContent = 'absolute: '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'static' );
offset.textContent = 'static: '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'static2' );
offset.textContent = 'static: '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'relative' );
offset.textContent = 'relative: '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'fixed-side' );
offset.textContent = 'fixed/absolute (side): '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'fixed-top' );
offset.textContent = 'fixed/absolute (top): '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.getElementById( 'fixed-bottom' );
offset.textContent = 'fixed/absolute (bottom): '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.querySelectorAll( '#relative-wrapped-absolute div' )[0];
offset.textContent = 'absolute/relative/static (absolute/relative wrapped): '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
offset = document.querySelectorAll( '#static-wrapped-static div' )[0];
offset.textContent = 'static (static wrapped): '
+ offset.documentOffsetTop + ', $'
+ $( offset ).offset().top;
HTML:
<div id="static" class="offset">0</div>
<div id="static2" class="offset">0</div>
<div id="static-wrapped-static"><br /><div class="offset">0</div></div>
<div id="absolute" class="offset">0</div>
<div id="relative" class="offset">0</div>
<div id="fixed-side" class="offset">0</div>
<div id="fixed-top" class="offset">0</div>
<div id="fixed-bottom" class="offset">0</div>
<div style="position: relative;"><div id="relative-wrapped-absolute"><div class="offset">0</div></div></div>
<div id="grid"><div class="grid-line">10</div></div>
CSS:
body {padding-left: 12px;}
#absolute {
top: 100px;
position: absolute;
}
#relative {
top: 150px;
position: relative;
}
#fixed-side {
right: 0;
position: fixed;
}
#fixed-top {
left: 50%;
position: fixed;
top: 0;
}
#fixed-bottom {
left: 50%;
position: fixed;
bottom: 0;
}
#relative-wrapped-absolute {
top: 8px;
position: relative;
}
#relative-wrapped-absolute div {
position: absolute;
top: 20px;
}
.offset {
border: 1px solid black;
}
#grid {
height: 100%;
left: 0;
position: absolute;
top: 1px;
width: 100%;
z-index: -1;
}
.grid-line {
border-bottom: 1px solid lightgray;
font-size: 8px;
height: 9px;
line-height: 20px;
}
输出:
链接地址: http://www.djcxy.com/p/15617.html上一篇: how to get the top left x and y coordinate using javascript