Created
January 8, 2018 23:18
-
-
Save nyawach/d055b1eaafc03524933c5aa43c1f45ff to your computer and use it in GitHub Desktop.
rootからtargetのスクロール量を計算する
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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