Skip to content

Instantly share code, notes, and snippets.

@nyawach
Created January 8, 2018 23:18
Show Gist options
  • Save nyawach/d055b1eaafc03524933c5aa43c1f45ff to your computer and use it in GitHub Desktop.
Save nyawach/d055b1eaafc03524933c5aa43c1f45ff to your computer and use it in GitHub Desktop.
rootからtargetのスクロール量を計算する
/**
* rootからtargetのスクロール量を計算する
*/
export default function calcOffsetTop(target: HTMLElement, root: HTMLElement = document.body) {
if(!target) throw new Error('[no target]');
let elm: HTMLElement = target
let offset: number = 0
while(true) {
offset += elm.offsetTop
if(elm.offsetParent === root) break
elm = elm.offsetParent as HTMLElement
}
return offset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment