How do we know the nearest positioned parent element to an absolute element? any js scripts or plugins available to play around
Asked
Active
Viewed 53 times
-1
-
[DUPLICATED] You can Find your answer here: [Is there a CSS parent selector?](https://stackoverflow.com/questions/1014861/is-there-a-css-parent-selector) – Mehdi Bouzidi Sep 19 '17 at 13:18
-
Questions asking us to suggest, find or recommend a book, tool, software library, plug-in, tutorial, explain a technique or provide any other off-site resource are off-topic for Stack Overflow Stack Overflow – Paulie_D Sep 19 '17 at 15:09
1 Answers
0
I would start playing with something like this:
function getPositionAnchor(startFromElm) {
var positionAnchor = null;
var validAnchors = ['relative','absolute','fixed'];
var elm = startFromElm.parentElement;
do {
if (validAnchors.indexOf(getComputedStyle(elm)['position']) !== -1
|| elm.tagName.toLowerCase() === 'html') {
positionAnchor = elm;
} else {
elm = elm.parentElement;
}
} while (!positionAnchor)
return positionAnchor;
}

Uri Kalish
- 13
- 4