简体   繁体   English

获取元素相对于浏览器的绝对位置

[英]Getting absolute position of an element relative to browser

I have a div set to the css class float with float being: 我有一个div设置为css类float与float是:

.float {
display:block; 
position:fixed; 
top: 20px;
left: 0px;
z-index: 1999999999;
}
* html .float {position:absolute;}

This class causes the element to stay in a fixed position on the page (the *html part is to make it work in IE). 此类使元素保持在页面上的固定位置(* html部分用于使其在IE中工作)。 I am using javascript to shift the position of the element horizontally and vertically. 我正在使用javascript将元素的位置水平和垂直移动。

I need to get the absolute position of the div relative to the browser window in javascript (how many pixels from the top and left of the browser window the div is). 我需要在javascript中获取div相对于浏览器窗口的绝对位置(div是从浏览器窗口的顶部和左侧开始的多少像素)。 Right now, I am using the following: 现在,我正在使用以下内容:

pos_left = document.getElementById('container').offsetLeft;
pos_top = document.getElementById('container').offsetTop;

The code above works for IE, Chrome, and FF, but in Opera it returns 0 for both. 上面的代码适用于IE,Chrome和FF,但在Opera中,两者均返回0。 I need a solution that works for all of those browsers. 我需要一种适用于所有这些浏览器的解决方案。 Any ideas? 有任何想法吗?

Btw: Keeping tracking of the changes made by javascript is possible, but that is not the solution I am looking for due to performance reasons. 顺便说一句:可以跟踪javascript所做的更改,但是由于性能原因,这不是我要寻找的解决方案。 Also, I am not using jquery. 另外,我使用jQuery。

I've had a similar question before, and looking through the blogs, the websites and this solution from stackoverflow, I've realized that a one size fits all solution is nowhere to be found! 之前我有一个类似的问题,通过博客,网站以及stackoverflow的此解决方案,我发现找不到适合所有解决方案的单一尺寸! So, without further ado, here is what I've put together that gets the x,y of any element through any parent node, including iframes, scrolling divs and whatever! 因此,事不宜迟,这是我拼凑而成的东西,它可以通过任何父节点(包括iframe,滚动div等)获取任何元素的x,y! Here ya go: 你去:

function findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if(obj.offsetLeft) curleft += parseInt(obj.offsetLeft);
    if(obj.offsetTop) curtop += parseInt(obj.offsetTop);
    if(obj.scrollTop && obj.scrollTop > 0) curtop -= parseInt(obj.scrollTop);
    if(obj.offsetParent) {
        var pos = findPos(obj.offsetParent);
        curleft += pos[0];
        curtop += pos[1];
    } else if(obj.ownerDocument) {
        var thewindow = obj.ownerDocument.defaultView;
        if(!thewindow && obj.ownerDocument.parentWindow)
            thewindow = obj.ownerDocument.parentWindow;
        if(thewindow) {
            if(thewindow.frameElement) {
                var pos = findPos(thewindow.frameElement);
                curleft += pos[0];
                curtop += pos[1];
            }
        }
    }

    return [curleft,curtop];
}

A variation on user1988451's answer that I have tested cross-browser. 我测试了跨浏览器的user1988451答案的一个变体。 I found their answer did not address scroll width (so I added the check for obj.scrollLeft), and behaved differently in FF and Chrome until I added the checks for thewindow.scrollY and thewindow.scrollX. 我发现他们的答案没有解决滚动宽度问题(因此我添加了对obj.scrollLeft的检查),并且在FF和Chrome中的行为有所不同,直到我为thewindow.scrollY和thewindow.scrollX添加了检查。 Tested in Chrome, IE, Safari, Opera, FF, IE 9-10, and IE9 with 8 and 7 modes. 在具有8和7模式的Chrome,IE,Safari,Opera,FF,IE 9-10和IE9中进行了测试。 Please note that I have not tested its behavior with iframes: 请注意,我尚未使用iframe测试其行为:

function findPos(obj, foundScrollLeft, foundScrollTop) {
    var curleft = 0;
    var curtop = 0;
    if(obj.offsetLeft) curleft += parseInt(obj.offsetLeft);
    if(obj.offsetTop) curtop += parseInt(obj.offsetTop);
    if(obj.scrollTop && obj.scrollTop > 0) {
        curtop -= parseInt(obj.scrollTop);
        foundScrollTop = true;
    }
    if(obj.scrollLeft && obj.scrollLeft > 0) {
        curleft -= parseInt(obj.scrollLeft);
        foundScrollLeft = true;
    }
    if(obj.offsetParent) {
        var pos = findPos(obj.offsetParent, foundScrollLeft, foundScrollTop);
        curleft += pos[0];
        curtop += pos[1];
    } else if(obj.ownerDocument) {
        var thewindow = obj.ownerDocument.defaultView;
        if(!thewindow && obj.ownerDocument.parentWindow)
            thewindow = obj.ownerDocument.parentWindow;
        if(thewindow) {
            if (!foundScrollTop && thewindow.scrollY && thewindow.scrollY > 0) curtop -= parseInt(thewindow.scrollY);
            if (!foundScrollLeft && thewindow.scrollX && thewindow.scrollX > 0) curleft -= parseInt(thewindow.scrollX);
            if(thewindow.frameElement) {
                var pos = findPos(thewindow.frameElement);
                curleft += pos[0];
                curtop += pos[1];
            }
        }
    }

    return [curleft,curtop];
}

I use this for PhantomJS rasterization: 我将其用于PhantomJS栅格化:

function getClipRect(obj) {
  if (typeof obj === 'string')
    obj = document.querySelector(obj);

  var curleft = 0;
  var curtop = 0;

  var findPos = function(obj) {
    curleft += obj.offsetLeft;
    curtop += obj.offsetTop;
    if(obj.offsetParent) {
      findPos(obj.offsetParent);
    }
  }
  findPos(obj);

  return {
    top: curtop,
    left: curleft,
    width: obj.offsetWidth,
    height: obj.offsetHeight
  };
}

If you can use items style element; 如果可以使用项目样式元素;

<div id="container" style="top: 20px;left: 0px;z-index: 1999999999;">

You can get it with element style attribute; 您可以使用元素样式属性来获取它;

var top = parseInt(document.getElementById('container').style.top.split('px')[0], 10); // This row returns 20

You can use top, left, width, height etc... 您可以使用顶部,左侧,宽度,高度等...

您可能会在这里找到线索: http : //code.google.com/p/doctype/wiki/ArticlePageOffset但我认为您需要添加父母的偏移量才能获得正确的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM