Retrieve the position (X,Y) of an HTML element relative to the browser window

前端 未结 27 3780
闹比i
闹比i 2020-11-21 04:59

I want to know how to get the X and Y position of HTML elements such as img and div in JavaScript relative to the browser window.

相关标签:
27条回答
  • 2020-11-21 05:41

    This is easy as two lines in JS :

    var elem = document.getElementById("id");    
    alert(elem.getBoundingClientRect());
    
    0 讨论(0)
  • 2020-11-21 05:42

    If page includes - at least- any "DIV", the function given by meouw throws the "Y" value beyond current page limits. In order to find the exact position, you need to handle both offsetParent's and parentNode's.

    Try the code given below (it is checked for FF2):

    
    var getAbsPosition = function(el){
        var el2 = el;
        var curtop = 0;
        var curleft = 0;
        if (document.getElementById || document.all) {
            do  {
                curleft += el.offsetLeft-el.scrollLeft;
                curtop += el.offsetTop-el.scrollTop;
                el = el.offsetParent;
                el2 = el2.parentNode;
                while (el2 != el) {
                    curleft -= el2.scrollLeft;
                    curtop -= el2.scrollTop;
                    el2 = el2.parentNode;
                }
            } while (el.offsetParent);
    
        } else if (document.layers) {
            curtop += el.y;
            curleft += el.x;
        }
        return [curtop, curleft];
    };
    
    
    0 讨论(0)
  • 2020-11-21 05:43

    If you want it done only in javascript, here are some one liners using getBoundingClientRect()

    window.scrollY + document.querySelector('#elementId').getBoundingClientRect().top // Y
    
    window.scrollX + document.querySelector('#elementId').getBoundingClientRect().left // X
    

    The first line will return offsetTop say Y relative to document. The second line will return offsetLeft say X relative to document.

    getBoundingClientRect() is a javascript function that returns the position of the element relative to viewport of window.

    0 讨论(0)
  • 2020-11-21 05:44

    To get the total offset of an element, you could recursively sum up all parent offsets:

    function getParentOffsets(el): number {
    if (el.offsetParent) {
        return el.offsetParent.offsetTop + getParentOffset(el.offsetParent);
    } else {
        return 0;
    }
    }
    

    with this utility function the total top offset of a dom element is:

    el.offsetTop + getParentOffsets(el);
    
    0 讨论(0)
  • 2020-11-21 05:45

    HTML elements on most browsers will have:-

    offsetLeft
    offsetTop
    

    These specifiy the position of the element relative its nearest parent that has layout. This parent can often be accessed bif the offsetParent property.

    IE and FF3 have

    clientLeft
    clientTop
    

    These properties are less common, they specify an elements position with its parents client area (padded area is part of the client area but border and margin is not).

    0 讨论(0)
  • 2020-11-21 05:45

    jQuery .offset() will get the current coordinates of the first element, or set the coordinates of every element, in the set of matched elements, relative to the document.

    0 讨论(0)
提交回复
热议问题