I have a scrollable div, the one on the left in the above image.
In this div there exist many elements, lets focus on one of them (highlighted in grey) and call it A.
The right image is a representation of the div with the full size (without scroll or width setting) where A is placed without scrolling.
How may I find the value of X (in green), while having the left div currently on the page.
i.e. taking into consideration:
i.e. How to get the relative y coordinate of an element inside a scrollable div ? in simple words: how to calculate x via javascript ?
To get or set the scroll position of an element, you follow these steps: First, select the element using the selecting methods such as querySelector() . Second, access the scroll position of the element via the scrollLeft and scrollTop properties.
The scrollTop() method sets or returns the vertical scrollbar position for the selected elements.
An element's scrollTop value is a measurement of the distance from the element's top to its topmost visible content. When an element's content does not generate a vertical scrollbar, then its scrollTop value is 0 .
Try this :
var elementTop = document.getElementById('yourElementId').offsetTop; var divTop = document.getElementById('yourDivId').offsetTop; var elementRelativeTop = elementTop - divTop;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With