Skip to content

Instantly share code, notes, and snippets.

@7cc
Created February 1, 2014 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7cc/8758153 to your computer and use it in GitHub Desktop.
Save 7cc/8758153 to your computer and use it in GitHub Desktop.

difference

$(sel).offset().top // jQuery
el.getBoundingClientRect().top + pageYOffset - document.documentElement.clientTop // pure JS

document.documentElement.clientTop?

html {
  border: 10px solid
}
browser document.documentElement.clientTop returns?
IE y
Chrome y
Firefox n

Don't use document.documentElement.clientTop

el.offsetTop?

Never use el.offsetTop

el.offsetTop
  returns distance from el.offsetParent
  e.g. <td> element's offsetParent is <tr>
el.clientTop
  inline -> 0
  block  -> border-top-width
@7cc
Copy link
Author

7cc commented Feb 3, 2014

just use el.getBoundingClientRect().top + pageYOffset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment