I want to know how to use JavaScript to get the distance of an element from the top of the page not the parent element.

- 4,406
- 9
- 39
- 66
-
2possible duplicate of [Determine distance from the top of a div to top of window with javascript](http://stackoverflow.com/questions/9880472/determine-distance-from-the-top-of-a-div-to-top-of-window-with-javascript) – T.Todua Sep 23 '15 at 11:26
16 Answers
var elDistanceToTop = window.pageYOffset + el.getBoundingClientRect().top
In my experience document.body.scrollTop
doesn't always return the current scroll position (for example if the scrolling actually happens on a different element).

- 1,988
- 1
- 14
- 10
-
6Unlike the top answer, this DOES work on iOS Safari. Don't use 2 hours on debugging like I did :) – Koodimetsa Apr 19 '20 at 19:38
-
offsetTop only looks at the element's parent. Just loop through parent nodes until you run out of parents and add up their offsets.
function getPosition(element) {
var xPosition = 0;
var yPosition = 0;
while(element) {
xPosition += (element.offsetLeft - element.scrollLeft + element.clientLeft);
yPosition += (element.offsetTop - element.scrollTop + element.clientTop);
element = element.offsetParent;
}
return { x: xPosition, y: yPosition };
}
UPDATE: This answer has some problems, values will have tiny differences compare to what it should be and will not work correctly in some cases.
Check @eeglbalazs's answer, which is accurate.

- 4,284
- 2
- 29
- 57

- 3,519
- 3
- 16
- 16
-
2element in this case would be something like: `var element = document.querySelectorAll('.wrapper')[0];` – Colton45 Sep 14 '17 at 00:34
-
-
1@Colton45 actually you should use `document.querySelector('.wrapper')` – Guilherme Nagatomo Dec 21 '17 at 15:59
-
2Just to further add to this, this implementation does not function the same in Safari as Chrome - therefore I would argue it should not be accepted. Here is a demo (scroll on both browsers): https://codepen.io/anon/pen/LaKEPK. This implementation https://stackoverflow.com/a/53351648/775007 works. – Dean Mar 27 '19 at 13:46
-
@Dean You sure about that? It's working for me in every brower and OS I have available. I think you might have confused yourself by using vh units in your css lol. https://codepen.io/anon/pen/zXqqbP – Shawn Whinnery Apr 04 '19 at 17:48
-
@ShawnWhinnery Yes i'm sure. I just tried your codepen and the behaviour is the same. The value changes as you scroll in one browser and doesn't in the other :) – Dean Apr 05 '19 at 19:15

- 2,521
- 1
- 29
- 47

- 30,730
- 8
- 78
- 73
-
15This returns distance from offsetParent. Is their a way to get it from the top of the page? – Sami Al-Subhi Aug 04 '12 at 04:58
-
In my testing that has given me the correct result every time. Do you have a case where it does not? – sachleen Aug 06 '12 at 03:10
-
9@sachleen, http://jsfiddle.net/HxDEt/1/ shows that your answer is incorrect if any container elements have relative, absolute, or fixed position (and have an offsetTop themselves). – Andrew Oct 24 '13 at 16:50
-
Here is some interesting code for you :)
window.addEventListener('load', function() {
//get the element
var elem = document.getElementById('test');
//get the distance scrolled on body (by default can be changed)
var distanceScrolled = document.body.scrollTop;
//create viewport offset object
var elemRect = elem.getBoundingClientRect();
//get the offset from the element to the viewport
var elemViewportOffset = elemRect.top;
//add them together
var totalOffset = distanceScrolled + elemViewportOffset;
//log it, (look at the top of this example snippet)
document.getElementById('log').innerHTML = totalOffset;
});
#test {
width: 100px;
height: 100px;
background: red;
margin-top: 100vh;
}
#log {
position: fixed;
top: 0;
left: 0;
display: table;
background: #333;
color: #fff;
}
html,
body {
height: 2000px;
height: 200vh;
}
<div id="log"></div>
<div id="test"></div>

- 4,960
- 3
- 31
- 56
offsetTop doesn’t get the distance to the top of the page, but rather to the top of the closest parent element that has a specified position.
You can use a simple technique that adds up the offsetTop of all the parent element of the element you are interested in to get the distance.
// Our element
var elem = document.querySelector('#some-element');
// Set our distance placeholder
var distance = 0;
// Loop up the dom
do {
// Increase our distance counter
distance += elem.offsetTop;
// Set the element to it's parent
elem = elem.offsetParent;
} while (elem);
distance = distance < 0 ? 0 : distance;
Original code from https://gomakethings.com/how-to-get-an-elements-distance-from-the-top-of-the-page-with-vanilla-javascript/

- 454
- 5
- 9
-
14It's surreal to Google how to do something and discover a StackOverflow answer that links back an article YOU wrote. LOL thanks for this! – Chris Ferdinandi Mar 12 '19 at 14:59
This oneliner seems to work nice
document.getElementById("el").getBoundingClientRect().top + window.scrollY

- 1,673
- 1
- 18
- 20
-
This should be top, the top answer is using a depreciated method +1 – MakingStuffs Jul 18 '23 at 18:01
var distanceTop = element.getBoundingClientRect().top;
For details vist a link:
https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect

- 21,981
- 30
- 95
- 142

- 91
- 1
- 1
-
1A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Filnor Mar 16 '18 at 09:41
-
7This will not give actual height from top. It returns the height from top at current scrolled state of the window. – blackspacer Jan 22 '19 at 13:03
**For anchor links (href="/#about"
) to anchor <div id="about">
read part 3.
1 (distance_from_top)
Less than 30 seconds solution (Two lines of code "hello world"):
get your element:
var element = document.getElementById("hello");
Get getBoundingClientRect ();
The Element.getBoundingClientRect() method returns the size of an element and its position relative to the viewport. https://developer.mozilla.org/en-US/docs/Web/API/Element/getBoundingClientRect
var rect = element.getBoundingClientRect();
Return object:
Dot notation top
var distance_from_top = rect.top; /* 1007.9971313476562 */
Thats it.
2 (window.scrollTo)
StackOverflow nightmare 2 - set scroll position to this value
Again "hello world" (8,000 answers out there - 7,999 not working or to complex).
https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollTo
window.scrollTo({
top: element.getBoundingClientRect().top,
behavior: 'smooth'
});
Add offset value to top
if you want (For sticky navbars).
"Hello World" code snippet (Get distance from top viewport + click to scrollTo
)
var element = document.getElementById("hello");
var rect = element.getBoundingClientRect();
var distance_from_top = rect.top; /* 50px */
console.log(distance_from_top);
function scrollTovView(){
window.scrollTo({
top: distance_from_top,
behavior: 'smooth'
});
}
div{
text-align:center;
border: 1px solid lightgray;
}
<button onclick="scrollTovView()">scrollTo to red DIV</button>
<div style="height: 50px;">50px height</div>
<div id="hello" style="width: 500px; height: 500px; background: red;"></div>
3 (scrollTo & anchors)
scrollTo "conflict" with main anchor navbars
This trick is very buggy if, for example, you use this URL:
www.mysite/about#hello
to
<div id="hello">hello</div>
top
is 0 or buggy (The HTML moves to hello
section).
window.scrollTo({
top: element.getBoundingClientRect().top,
behavior: 'smooth'
});
For this code to work you should add:
if (this.hash !== "") {
// Prevent default anchor click behavior
event.preventDefault();
Basic example her: https://www.w3schools.com/howto/howto_css_smooth_scroll.asp

- 6,887
- 2
- 25
- 37
Although it is quite an old discussion, but this works pretty well on chrome / firefox / safari
browsers:
window.addEventListener('scroll', function() {
var someDiv = document.getElementById('someDiv');
var distanceToTop = someDiv.getBoundingClientRect().top;
});
Check it out on JSFiddle

- 920
- 1
- 12
- 22
scroll to element's top position;
var rect = element.getBoundingClientRect();
var offsetTop = window.pageYOffset + rect.top - rect.height;

- 21
- 2
(SOURCE : Determine distance from the top of a div to top of window with javascript )
<script type="text/javascript">
var myyElement = document.getElementById("myyy_bar"); //your element
var EnableConsoleLOGS = true; //to check the results in Browser's Inspector(Console), whenever you are scrolling
// ==============================================
window.addEventListener('scroll', function (evt) {
var Positionsss = GetTopLeft ();
if (EnableConsoleLOGS) { console.log(Positionsss); }
});
function GetOffset (object, offset) {
if (!object) return;
offset.x += object.offsetLeft; offset.y += object.offsetTop;
GetOffset (object.offsetParent, offset);
}
function GetScrolled (object, scrolled) {
if (!object) return;
scrolled.x += object.scrollLeft; scrolled.y += object.scrollTop;
if (object.tagName.toLowerCase () != "html") { GetScrolled (object.parentNode, scrolled); }
}
function GetTopLeft () {
var offset = {x : 0, y : 0}; GetOffset (myyElement, offset);
var scrolled = {x : 0, y : 0}; GetScrolled (myyElement.parentNode, scrolled);
var posX = offset.x - scrolled.x; var posY = offset.y - scrolled.y;
return {lefttt: posX , toppp: posY };
}
// ==============================================
</script>
This function returns distance from top of the page, even if your window is scrolled. It can be used in event listeners.
const getElementYOffset = (element) => {
const scrollOnWindow =
window.pageYOffset !== undefined
? window.pageYOffset
: (document.documentElement || document.body.parentNode || document.body)
.scrollTop;
const rect = element.getBoundingClientRect();
let distanceFromTopOfPage = rect.top;
if (scrollOnWindow !== 0) {
distanceFromTopOfPage = rect.top + scrollOnWindow;
}
return distanceFromTopOfPage;
};

- 3,752
- 35
- 31
- 35

- 11
- 2
You only need this line
document.getElementById("el").getBoundingClientRect().top
in which "el" is the element.

- 11
- 1
-
Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 14 '22 at 00:28
Since window.pageYOffset
is a legacy alias of window.scrollY
, eeglbalazs answer can be improved to:
const elDistanceToTop = window.scrollY + el.getBoundingClientRect().top;

- 876
- 9
- 23