Skip to content

Instantly share code, notes, and snippets.

@nt1m
Created March 7, 2017 04:19
Show Gist options
  • Save nt1m/06910eae5ebe45865b18ce56a2b3aebc to your computer and use it in GitHub Desktop.
Save nt1m/06910eae5ebe45865b18ce56a2b3aebc to your computer and use it in GitHub Desktop.
function getPositionReference(node) {
let pos = getComputedStyle(node).getPropertyValue("position");
switch (pos) {
case "relative":
return node;
case "absolute":
let parentPosition = "static";
let parent = node.parentNode;
while (parentPosition == "static" && parent.parentNode) {
parent = parent.parentNode;
}
return parent;
case "fixed":
return window; // corresponds to the viewport, not to any element...
case "sticky":
// this acts like position: relative in some cases and position: fixed in others
// mainly depends on scroll position
// TODO.
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment