javascript - How to get placement coordinates of an absolutely positioned element relative to another? -
i can't seem find on github here go:
suppose have dom element parent
contains position: relative
, element child
contains position: absolute
, child of parent, how can go getting coordinates [top, left] child element [relative parent] given placement string? (i.e. "top left", "top middle", "bottom right", , forth... ?)
ideally, this:
var coordinates = getrelativecoordinates({ el: child // child element contains "position: absolute" relativetoel: parent // parent container contains "position: relative" placement: "top left" // "top left" || "top middle" || "top right" || "right top" || etc... }); console.log(coordinates); >> {top: "-100px", left: "0px"}
"when position set absolute, element removed normal document flow—that is, no longer takes space , may overlap other elements. also, top , left properties can used absolutely position relative top-left corner of nearest enclosing element position property isn’t static, or relative document if no such enclosing element exists." - http://eloquentjavascript.net/13_dom.html
then need compute: if know .width
, .height
properties, can compute different places adding top left amount of width or height.
ex. (top middle) = ((parseint(child.style.left) + parseint(child.style.width)/2)
, parseint(child.style.top)
) (x, y)
note parseint
used number out "px".
you need write out each possible location computation this. store them array [left, top] or in object {left: , top: }.
Comments
Post a Comment