Last active
June 26, 2018 15:49
-
-
Save harmoniemand/35b56895a14be1bf1727ff81f5206757 to your computer and use it in GitHub Desktop.
Calculate Offset in HTML while using offset and scrolling
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
/* | |
License: MIT (https://opensource.org/licenses/MIT) | |
*/ | |
GetOffset(from: any, to: any): PositionModel { | |
let pos = new PositionModel(); | |
var parent = from.offsetParent; | |
pos.X = from.offsetTop; | |
pos.Y = from.offsetLeft; | |
var el = from; | |
while (el != parent) { | |
pos.X = pos.X - el.scrollTop; | |
pos.Y = pos.Y - el.scrollLeft; | |
el = el.parentNode; | |
} | |
if (parent != to) { | |
const parentOffset = this.GetOffset(parent, to); | |
pos.X = pos.X + parentOffset.X; | |
pos.Y = pos.Y + parentOffset.Y; | |
} | |
return pos; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment