After trying it all night, I seem to have found a way.
My mind is still confused now, so write it down first and analyze it later.
// Find the relative position of the child element in the parent element
function getElementTop(element){
var el = (typeof element == "string") ? document.getElementById(element) : element;
if (el.parentNode === null || el. style.display == 'none') {
return false;
}
return el.offsetTop - el.parentNode.offsetTop;
}
This function can Get the relative height of the child element in the parent element. You can position the child element by setting the scrollTop attribute of the parent element.