How to get an element's top position relative to the browser's viewport?

后端 未结 12 956
你的背包
你的背包 2020-11-29 17:17

I want to get the position of an element relative to the browser\'s viewport (the viewport in which the page is displayed, not the whole page). How can this be done in JavaS

相关标签:
12条回答
  • 2020-11-29 17:43

    Thanks for all the answers. It seems Prototype already has a function that does this (the page() function). By viewing the source code of the function, I found that it first calculates the element offset position relative to the page (i.e. the document top), then subtracts the scrollTop from that. See the source code of prototype for more details.

    0 讨论(0)
  • 2020-11-29 17:47

    I am assuming an element having an id of btn1 exists in the web page, and also that jQuery is included. This has worked across all modern browsers of Chrome, FireFox, IE >=9 and Edge. jQuery is only being used to determine the position relative to document.

    var screenRelativeTop =  $("#btn1").offset().top - (window.scrollY || 
                                                window.pageYOffset || document.body.scrollTop);
    
    var screenRelativeLeft =  $("#btn1").offset().left - (window.scrollX ||
                                               window.pageXOffset || document.body.scrollLeft);
    
    0 讨论(0)
  • 2020-11-29 17:49

    The existing answers are now outdated. The native getBoundingClientRect() method has been around for quite a while now, and does exactly what the question asks for. Plus it is supported across all browsers (including IE 5, it seems!)

    From MDN page:

    The returned value is a TextRectangle object, which contains read-only left, top, right and bottom properties describing the border-box, in pixels, with the top-left relative to the top-left of the viewport.

    You use it like so:

    var viewportOffset = el.getBoundingClientRect();
    // these are relative to the viewport, i.e. the window
    var top = viewportOffset.top;
    var left = viewportOffset.left;
    
    0 讨论(0)
  • 2020-11-29 17:51

    On my case, just to be safe regarding scrolling, I added the window.scroll to the equation:

    var element = document.getElementById('myElement');
    var topPos = element.getBoundingClientRect().top + window.scrollY;
    var leftPos = element.getBoundingClientRect().left + window.scrollX;
    

    That allows me to get the real relative position of element on document, even if it has been scrolled.

    0 讨论(0)
  • 2020-11-29 17:54

    Sometimes getBoundingClientRect() object's property value shows 0 for IE. In that case you have to set display = 'block' for the element. You can use below code for all browser to get offset.

    Extend jQuery functionality :

    (function($) {
        jQuery.fn.weOffset = function () {
            var de = document.documentElement;
            $(this).css("display", "block");
            var box = $(this).get(0).getBoundingClientRect();
            var top = box.top + window.pageYOffset - de.clientTop;
            var left = box.left + window.pageXOffset - de.clientLeft;
            return { top: top, left: left };
        };
    }(jQuery));
    

    Use :

    var elementOffset = $("#" + elementId).weOffset();
    
    0 讨论(0)
  • 2020-11-29 17:55

    This way is also applicable for calling in JAVA:

          Object bound = driver.executeScript("var bounding = arguments[0].getBoundingClientRect();" +
          "return bounding.top >= 0 && " +
          "        bounding.left >= 0 && " +
          "        bounding.bottom <= (window.innerHeight || document.documentElement.clientHeight) && " +
          "        bounding.right <= (window.innerWidth || document.documentElement.clientWidth)",
        element);
    
      boolean isElementInView = ((Boolean)bound).booleanValue(); //returns true if element in viewport
    
    0 讨论(0)
提交回复
热议问题