Follow The Menu: A jQuery Experiment In Position Animation

Posted on Feb 16, 2009

I was working with jQuery today and built a simple menu tracker in which a dot animates to whatever menu item is currently being hovered over, which you can see here:

All I did was make a container div and then two child div elements, one for the menu items and one for the ball. You can see that if you view source on the HTML.I did the menu items as a ul with a collection of li tags with list-style set to none. Not being super CSS savvy, it took some futzing with getting my positioning right until I realized that all I needed was to have position:absolute specified for my little ball.

Then, all I had to do was animate the left property of the of the ball using jQuery. It is super handy in jQuery that you can get the position of any element so that you can dynamically pass that to your animation. So, what I do is get the position of the li and then subtract its width divided by two so that the ball is cetnered underneath each menu item. I also set the ball’s initial position to the first item. Here’s the jQuery code: