javascript - How to find position of a web element in relation to another web element? -
i have scenario have assert presence of web element position. more clear have web element x , web element y. have assert after clicking button can see x sitting above y. how that?
you should 2 elements‘ positions in document , subtract position value of x value of y. if value positive, know x sitting farther y.
getting position
use
boxpositionofx = getelementbyid(“myx”).getboundingclientrect(); boxpositionofy = getelementbyid(“myy”).getboundingclientrect();
this returns object containing 4 position values: top
, right
, bottom
, left
, relative viewport. not need convert viewport positions document positions, since document offset you'd have add same both elements , cut in subtraction.
comparing positions
now subtract positions:
positiondifference = boxpositionofy.top - boxpositionofx.top;
if positiondifference
greater 0, x’s top border lies above y’s top border.
if want make sure elements not overlap, use this:
if ( (boxpositionofy.top - boxpositionofx.bottom) >= 0) { alert("x above y. not overlap."); }
Comments
Post a Comment