Document.elementFromPoint
Reacting to events with JavaScript is the foundation of a dynamic experiences on the web. Whether it's a click
event or another typical action, responding to that action is important. We started with assigning events to specific elements, then moved to event delegation for efficiency, but did you know you can identify elements by position on the page? Let's look at document.elementFromPoint
and document.elementsFromPoint
.
The document.elementFromPoint
method accepts x
and y
parameters to identify the top-most element at a point:
const element = document.elementFromPoint(100, 100);
//
If you want to know the entire element stack, you can use document.elementsFromPoint
:
const elements = document.elementsFromPoint(100, 100);
// [
The elementFromPoint
and elementsFromPoint
are really helpful for experiences where developers don't want to assign individual events. Games and entertainment sites could benefit from these functions. How would you use them?
![Welcome to My New Office]()
My first professional web development was at a small print shop where I sat in a windowless cubical all day. I suffered that boxed in environment for almost five years before I was able to find a remote job where I worked from home. The first...
![Regular Expressions for the Rest of Us]()
Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...
![Animated 3D Flipping Menu with CSS]()
CSS animations aren't just for basic fades or sliding elements anymore -- CSS animations are capable of much more. I've showed you how you can create an exploding logo (applied with JavaScript, but all animation is CSS), an animated Photo Stack, a sweet...
![Create a Twitter AJAX Button with MooTools, jQuery, or Dojo]()
There's nothing like a subtle, slick website widget that effectively uses CSS and JavaScript to enhance the user experience. Of course widgets like that take many hours to perfect, but it doesn't take long for that effort to be rewarded with above-average user retention and...