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

后端 未结 12 1208
你的背包
你的背包 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: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
    

提交回复
热议问题