[AngularJS] Get Element Offset (Position)

AngularJS doesn't provide offset() in jqLite. If you don't want to include jQuery for only offset(), the following is a possible implementation of offset():

function offset(elm) {
  try {return elm.offset();} catch(e) {}
  var rawDom = elm[0];
  var _x = 0;
  var _y = 0;
  var body = document.documentElement || document.body;
  var scrollX = window.pageXOffset || body.scrollLeft;
  var scrollY = window.pageYOffset || body.scrollTop;
  _x = rawDom.getBoundingClientRect().left + scrollX;
  _y = rawDom.getBoundingClientRect().top + scrollY;
  return { left: _x, top: _y };

This function takes one argument, which is the element wrapped with jqLite. A try-catch block is added in the beginning to use the offset() of jQuery if jQuery is included.


[1]getBoundingClientRect method