1

I need to know what element sits at the very bottom of the page (usually a <footer> but it can be something else). So I know I can use document.elementFromPoint() but only if the user has scrolled to the bottom because that function uses client coordinates not page coordinates. See my example below, both elements should have green text but only one does.

Is there a way to find an element at a given page coordinate?

$(document).ready(function() {
  var body = document.body,
      html = document.documentElement;

  var height = Math.max( body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight );

  // Find "Top element"
  $(document.elementFromPoint(10, 30)).css('color', 'green');

  // Find "Bottom element"
  $(document.elementFromPoint(10, height-10)).css('color', 'green');
});
html, body {
  margin: 0;
  padding: 0;
}

body {
  height: 2000px;
  position: relative;
}

h1 {
  background-color: blue;
  display: block;
  width: 100%;
}

.footer-ele {
  position: absolute;
  bottom: 0;
  background-color: red;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h1>Top element</h1>

<h1 class="footer-ele">Bottom element</h1>
sazr
  • 24,984
  • 66
  • 194
  • 362
  • Footers have always been very difficult. This might help. https://stackoverflow.com/questions/3942776/using-jquery-to-find-an-element-at-a-particular-position The footer does not have green text. The .footer-ele class inherits black text.

    Bottom element

    – react_or_angluar Oct 30 '20 at 23:30
  • You could gather all elements, get the bounding box for each and check if the point is within any of those bounding boxes. – Ouroborus Oct 31 '20 at 05:01

0 Answers0