Skip to content

Home

Vertical offset of element

Finds the distance from a given element to the top of the document.

const getVerticalOffset = el => {
  let offset = el.offsetTop,
    _el = el;
  while (_el.offsetParent) {
    _el = _el.offsetParent;
    offset += _el.offsetTop;
  }
  return offset;
};

getVerticalOffset('.my-element'); // 120

More like this

Start typing a keyphrase to see matching snippets.