15.8.3 Determining the Element at a Point
The getBoundingClientRect() method allows us to determine the current position of an element in a viewport. Sometimes we want to go in the other direction and determine which element is at a given location in the viewport. You can determine this with the elementFromPoint() method of the Document object. Pass X and Y coordinates (using viewport coordinates, not document coordinates) and this method returns an Element object that is at the specified position. At the time of this writing, the algorithm for selecting the element is not specified, but the intent of this method is that it returns the innermost and uppermost (see the CSS z-index attribute in §188.8.131.52 ) element at that point. If you specify a point that is outside of the viewport, elementFromPoint() will return null even if that point would be perfectly valid when converted to document coordinates.
elementFromPoint()seems like a very useful method, and the obvious use case is passing the coordinates of the mouse pointer to determine which element the mouse is over. As we’ll learn in Chapter 17 , however, mouse event objects already include this information in their target property. In practice, therefore, elementFromPoint() is not commonly used.欢迎转载,转载请注明来自一手册:http://yishouce.com/book/1/31433.html