I'm facing a challenge in determining the vertical position of an element using scrollTop
when the parent container has a position: fixed
.
When I use scrollTop, it consistently returns a value of 0
. This probably occurs because the element is no longer part of the regular flow. Am I overlooking something obvious or is there an alternative method to achieve this while maintaining position: fixed
and avoiding jQuery? Perhaps there is a way to ascertain the position of an element relative to its parent?
Find below the test code snippet.
document.getElementById('target').scrollTop;
.container {
padding-top: 1200px;
position: fixed;
top: 0;
right: 0;
bottom: 0;
left: 0;
background-color: orange;
}
#element {
background-color: green;
}
<div class="container">
<div id="target">
Target
</div>
</div>