Forgive me if my question seems trivial, but I'm a noob to web programming.
The use case I have is the following: I'm displaying a webpage in my mobile app through WKWebView and need to find the exact coordinates of an element on the webpage once the webpage finishes loading. I plan on having a floating button appear if the element is visible by listening to scrollViewDidScroll and comparing that with the element's coordinates.
I've done so by injecting the following JavaScript into the WebView:
function f() {
var targetElement = document.getElementById("target_element");
return targetElement.offsetTop;
}
if (document.readyState == 'complete') {
f();
}
I inject this and retrieve the y-coordinates from the top of the document when the WebView finishes loading, but I've noticed that this value will sometimes change.
Is there a way to retrieve the static coordinates of the target element just once or do I need to retrieve the most up-to-date coordinates whenever scrollViewDidScroll
is called? Also, when does offsetTop
change?