Day 26 of #javascript30 is making a dropdown menu that follows you along.
This lesson takes what I learned on day 22 about getBoundingClientRect() and uses it to move around a box behind the contents in a dropdown menu. We use mouseenter and mouseleave to react to the items hovered over.
Using opacity for "animation"
I learned a really cool trick from this lesson. To get a cool "animated" feel, first add a class that displays block on the hovered item, and then a setTimeout() with about 150 ms delay that adds another class that sets opacity
to 1. The item should have a transition
set in CSS too to make it really look good.
A gotcha when using this approach is that on mouseleave
, when you remove classes that keeps a list item open — you might get there "early" because of the timeout in the mouseenter function. So in the mouseenter
function, you can simply check if the other classes that don't use the timeout are present before adding it.