Search code examples
javascriptdomelementviewportgetboundingclientrect

How to check if event clientx, clienty is present within getBoundingClientRect?


I have an xy object from an mouseup event and I want to know if the xy is present within boundingClientRect of an element in the viewport. I am able to get left, right, top, bottom values of the element from getBoundingClientRect method. I want to know whether xy is within the element rect.


Solution

  • It sounds like you're talking about the clientX and clientY properties of a mouse event. If this is right, then the point and the rect are relative to the same coordinate area, and the following is true:

    If x is between left and right and y between bottom and top, the point is in the box.