方法一:
var odiv=documentgetElementByIdx_x('divid');
alert(odivgetBoundingClientRect()left);
alert(odivgetBoundingClientRect()top);
方法二:
function CPos(x, y)
{
thisx = x;
thisy = y;
}
function GetObjPos(ATarget)
{
var target = ATarget;
var pos = new CPos(targetoffsetLeft, targetoffsetTop);
var target = targetoffsetParent;
while (target)
{
posx += targetoffsetLeft;
posy += targetoffsetTop;
target = targetoffsetParent
}
return pos;
}
var obj = documentgetElementByIdx_x('divid')
alert(GetObjPos(obj)['x']) //x坐标
alert(GetObjPos(obj)['y']) //y坐标
方法三:
function getElementPos(elementId){
var ua = navigatoruserAgenttoLowerCase();
var isOpera = (uaindexOf('opera') != -1);
var isIE = (uaindexOf('msie') != -1 && !isOpera); // not opera spoof
var el = documentgetElementByIdx_x(elementId);
if (elparentNode === null || elstyledisplay == 'none') {
return false;
}
var parent = null;
var pos = [];
var box;
if (elgetBoundingClientRect) //IE
{
box = elgetBoundingClientRect();
var scrollTop = Mathmax(documentdocumentElementscrollTop, documentbodyscrollTop);
var scrollLeft = Mathmax(documentdocumentElementscrollLeft, documentbodyscrollLeft);
return {
x: boxleft + scrollLeft,
y: boxtop + scrollTop
};
}
else
if (documentgetBoxObjectFor) // gecko
{
box = documentgetBoxObjectFor(el);
var borderLeft = (elstyleborderLeftWidth) parseInt(elstyleborderLeftWidth) : 0;
var borderTop = (elstyleborderTopWidth) parseInt(elstyleborderTopWidth) : 0;
pos = [boxx - borderLeft, boxy - borderTop];
}
else // safari & opera
{
pos = [eloffsetLeft, eloffsetTop];
parent = eloffsetParent;
if (parent != el) {
while (parent) {
pos[0] += parentoffsetLeft;
pos[1] += parentoffsetTop;
parent = parentoffsetParent;
}
}
if (uaindexOf('opera') != -1 || (uaindexOf('safari') != -1 && elstyleposition == 'absolute'))
{
pos[0] -= documentbodyoffsetLeft;
pos[1] -= documentbodyoffsetTop;
}
}
if (elparentNode) {
parent = elparentNode;
}
else {
parent = null;
}
while (parent && parenttagName != 'BODY' && parenttagName != 'HTML') { // account for any scrolled
ancestors
pos[0] -= parentscrollLeft;
pos[1] -= parentscrollTop;
if (parentparentNode) {
parent = parentparentNode;
}
else {
parent = null;
}
}
return {
x: pos[0],
y: pos[1]
};
}
var xd = getElementPos("divid");
alert(xdx);
alert(xdy);
稍等上代码!!
<html><head>
<script type="text/javascript">
function show_coords(event){
var x = eventclientX;
var y = eventclientY;
var say = documentall("coords");
sayinnerHTML = "X:"+x+" Y:"+y;
saystyleposition = "absolute";
saystyleleft = x + 30;
saystyletop = y;
}
</script>
</head>
<body onmousemove="show_coords(event)">
<p id="coords"></p>
</body>
<html>
希望我的回答对你有用,有用就采纳!!!谢谢!
应该把放在一个div里,然后获取这个div的坐标值就可以了,
放到div里,给它个id
<div id=kk>
</div>
js代码中加入
var ks;
ks=kkinnerhtml;
之后过虑这个字符串
以上就是关于如何用JS 获取DIV的坐标位置全部的内容,包括:如何用JS 获取DIV的坐标位置、js如何获取鼠标在某元素移动时~鼠标指针在元素中的坐标、如何用JS获取ID为img1图片的 X与Y的坐标值等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)