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

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).

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)