Or the [`$.fn.offset()`][1] from Zepto.js might be a good starting point. Have a look at it and you'll see the recursion [on line 643][2].

[1]: https://github.com/madrobby/zepto/blob/master/src/zepto.js#L639
[2]: https://github.com/madrobby/zepto/blob/master/src/zepto.js#L643