js: Add jQuery plugin `offsetTopRelativeTo($ancestor)`

This commit is contained in:
Johannes Meyer 2019-12-13 16:11:24 +01:00
parent f4edcecca0
commit f5ce5d42ef
1 changed files with 21 additions and 0 deletions

View File

@ -67,4 +67,25 @@
return o;
};
$.fn.offsetTopRelativeTo = function($ancestor) {
if (typeof $ancestor === 'undefined') {
return false;
}
var el = this[0];
var offset = el.offsetTop;
var $parent = $(el.offsetParent);
if ($parent.is('body') || $parent.is($ancestor)) {
return offset;
}
if (el.tagName === 'TR') {
// TODO: Didn't found a better way, this will probably break sooner or later
return $parent.offsetTopRelativeTo($ancestor);
}
return offset + $parent.offsetTopRelativeTo($ancestor);
};
})(jQuery);