When it comes to finding relationships between elements, we traditionally think of a top-down approach. We can thank CSS and querySelector
/querySelectorAll
for that relationship in selectors. What if we want to find an element’s parent based on selector?
To look up the element tree and find a parent by selector, you can use HTMLElement
‘s closest
method:
// Our sample element is an "a" tag that matches ul > li > a const link = document.querySelector('li a'); const list = a.closest('ul');
closest
looks up the ancestor chain to find a matching parent
element — the opposite of traditional CSS selectors. You can provide closest
a simple or complex selector to look upward for!
Responsive and Infinitely Scalable JS Animations
Back in late 2012 it was not easy to find open source projects using
requestAnimationFrame()
– this is the hook that allows Javascript code to synchronize with a web browser’s native paint loop. Animations using this method can run at 60 fps and deliver fantastic…Creating Scrolling Parallax Effects with CSS
Introduction For quite a long time now websites with the so called “parallax” effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a…
Adding Events to Adding Events in MooTools
Note: This post has been updated. One of my huge web peeves is when an element has click events attached to it but the element doesn’t sport the “pointer” cursor. I mean how the hell is the user supposed to know they can/should click on…
CSS Vertical Center with Flexbox
I’m 31 years old and feel like I’ve been in the web development game for centuries. We knew forever that layouts in CSS were a nightmare and we all considered flexbox our savior. Whether it turns out that way remains to be seen but flexbox does easily…
Source link