to get the height offset of the element according to body, write the following function:
function getTop(ele){
var top=0;//
function getEleTop(ele){
top+=ele.offsetTop; //topeleoffsetTop
if(ele.tagName.toLowerCase()=="body"){ //elebodytop
return top;
}else{
getEleTop(ele.offsetParent); //eleoffsetTop
}
}
return getEleTop(ele);
}
the outer getTop () wraps the inner getEleTop (),. I want to hide the top variable so as not to be exposed.
Inner getEleTop (), I intended to use top to store the offsetTop, of ele and then use top to overlay the ele"s positioning parent element (assuming faEle) relative to its parent element (assuming paEle). Until it is superimposed on body.
this allows you to get the height offset of ele from body.
finally, the inner function returns top.
when I use the outer function in return, I call the inner getEleTop (ele), and end up not getting the value of top.
after some debugging, I found that the inner function didn"t use return top. So the outer function returns undefied.
if the outer function is directly return top, it can be returned accurately.
but why didn"t the inner function return top?
I hope all the great gods will explain it! Thank you!