Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the distance between HTML element and browser (or window) sides

How to find the distance in pixels between html element and one of the browser (or window) sides (left or top) using jQuery?

like image 456
Kai Avatar asked Jan 05 '11 07:01

Kai


People also ask

How do you find the distance between two elements in HTML?

offsetTop; distance = Math. sqrt(distX*distX + distY*distY); alert(Math. floor(distance));

What JavaScript method tells you how far an element is from the window?

You can use . offset() to get the offset compared to the document element and then use the scrollTop property of the window element to find how far down the page the user has scrolled: var scrollTop = $(window).

How do you find the distance from the top of the window to the element?

To get the distance from the top for an element with JavaScript, we get the sum of the window. pageYOffset property and the element's top value. const elDistanceToTop = window. pageYOffset + el.

How do I find the offset on my bottom?

The offset bottom is calculating by using this statement “var bottom = el. offsetTop + $(el). outerHeight();” and calculated value is displaying.


2 Answers

You can use the offset function for that. It gives you the element's position relative to the (left,top) of the document:

var offset = $("#target").offset(); display("span is at " + offset.left + "," + offset.top +    " of document"); 

Live example On my browser, that example says that the span we've targeted is at 157,47 (left,top) of the document. This is because I've applied a big padding value to the body element, and used a span with a spacer above it and some text in front of it.

Here's a second example showing a paragraph at the absolute left,top of the document, showing 0,0 as its position (and also showing a span later on that's offset from both the left and top, 129,19 on my browser).

like image 182
T.J. Crowder Avatar answered Sep 22 '22 08:09

T.J. Crowder


jQuery.offset needs to be combined with scrollTop and scrollLeft as shown in this diagram:

viewport scroll and element offset

Demo:

function getViewportOffset($e) {    var $window = $(window),      scrollLeft = $window.scrollLeft(),      scrollTop = $window.scrollTop(),      offset = $e.offset();    return {      left: offset.left - scrollLeft,      top: offset.top - scrollTop    };  }  $(window).on("load scroll resize", function() {    var viewportOffset = getViewportOffset($("#element"));    $("#log").text("left: " + viewportOffset.left + ", top: " + viewportOffset.top);  });
body { margin: 0; padding: 0; width: 1600px; height: 2048px; background-color: #CCCCCC; }  #element { width: 384px; height: 384px; margin-top: 1088px; margin-left: 768px; background-color: #99CCFF; }  #log { position: fixed; left: 0; top: 0; font: medium monospace; background-color: #EEE8AA; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>    <!-- scroll right and bottom to locate the blue square -->  <div id="element"></div>  <div id="log"></div>
like image 37
Salman A Avatar answered Sep 26 '22 08:09

Salman A