The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript jquery animated menu (image reposition/fade)

  • This topic is empty.
Viewing 15 posts - 1 through 15 (of 17 total)
  • Author
  • #33743

    Hi, all. Long time listener, first time caller. I’m a cut/paste/mimic weekender, and am fairly new to jquery. I’m looking for some expert help. Here’s a demo page with the concept: (zipped demo package:

    As you can see, this is an image-based WordPress navigation system with each menu item hanging from a string. The selected item (.current-menu-item) hangs lower than the others. That worked fine with image sprites/CSS, but I wanted to get fancy and add jquery to have the items animate as they move up/down on hover. I got it to work, but am running into a few issues. I’m hoping an expert can help:

    (1) The first time an item is hovered, it scrolls through the whole image before re-positioning properly. Can that be avoided?

    (2) The .current-menu-item class seems to get over-ridden by the main class after hovering. In this case, you can see that home starts out lower, but, after hovering, goes back to the height of the original images (until refreshing). Is there a more elegant/effective way to deal with the sub-class?

    Now, in the don’t-push-your-luck category:

    For the selected item, it doesn’t make sense to have it move up/down (since it’s selected). I thought I’d have it stay stationary (over-riding the main class’ animation) but fade to black/white. I embedded into the demo a section from another tutorial on fading. It works, but I couldn’t figure out how to apply that to a sub-class — see (2).

    I know there a lot here. I pushed this as far as I could before reaching out. Any help appreciated! Thanks.


    UPDATE: I never knew about jsfiddle. That makes this a lot easier. Here you go:


    FURTHER UPDATE: Using a slightly different technique, I solved most of my issues. Here’s the fiddle:

    In this example, “About” has been assigned an extra class (“current-menu-item”), which allows it to (1) hang lower to show that it is “active” and (2) react differently on hover with the subtle 3px nudge.

    I would like to darken or convert to gray scale that active menu item when it is hovered (to show — hey, you can’t really pick this…you’re already there). I’ve seen a couple image fade techniques using different background positions (and the sprite I’m using does have a grayscale version — background-position: 0 -279px; ). But with all my fiddling, I have not been able to add to this otherwise working version the fade effect. Any help appreciated.


    If I had to do that, (in JS) I would animate the objects instead of animating the CSS properties. But I would probably just use the CSS3 transition property on the background position. It wouldn’t animate in IE, but it would look fine (the items would jump down on hover instead of slide down).

    You’re forced to use a lot of javascript because you’re animating the CSS on each item. It would be much more efficient to use one JS function to affect them all properly.


    Thank you for the response. I contemplated CSS3, but wanted cross browser support. At least for the animation. I suppose I would settle for non-IE support on the fading.

    As for the multiple classes and heavy amount of JS, you’re right. The problem is that each of the images has a different horizontal position. Although the vertical offset is a fixed amount for each of the sprite images, I didn’t think I could use one function because of the horizontal differences. Can I?


    Why don’t you set the backgrounds via css and instead of changing the background position to give it the effect, you could physically move the items up and down and have overflow: hidden set to the parent.

    this would allow you to do something like

    $(this).animate({top: '0'});
    }, function(){
    $(this).animate({top: '-50px'});;

    Each of them are doing the same movements, so they should use the same functions.

    I try to keep CSS out of the javascript as much as possible.


    Well that would be MUCH better. I just messed with it a bit, but it’s not taking. I’m sure I’m just doing something silly wrong. Here’s the test fiddle:

    The hover effect you’re seeing (black/white) is the fall-back (non-JS) hover.

    Thanks for the help. I really appreciate it!


    I removed the unnecessary things in the HTML – Just merge this with your index.html file.

    The hover states, with the greyed background, isn’t done yet.

    But other than that, I think it’s far more simple!


    Wow! That’s so much cleaner. Thank you so much!

    I updated the fiddle ( to include stops, duration, and the selected item “nudge” (for the .current-menu-item class). This all seems great with jquery loading.

    But it doesn’t deprecate nicely. If I un-rem the a:hover classes from CSS it will nicely deprecate by showing gray hovers. But if I leave it un-remmed and turn jquery back on, I get a mix — animation and hovers turning gray. I’d like to avoid that. Any thoughts on that?

    Thanks again!


    So you only want it to go grey if javascript is disabled?


    In the face of your generosity, I at least owe you something clearer than I wrote above. Sorry. Here’s what I was hoping ideally would happen with js enabled/disabled compared against the current fiddle (

    – all of the non-.current-menu-item links hang high and move down on hover, with callback (DONE)
    – the .current-menu-item link hangs lower and nudges a few px on hover, with callback (DONE)
    – the .current-menu-item link does something else to indicate that clicking it will do nothing (I envisioned destaurating to greyscale, but haven’t been able to execute to decide just how (NOT DONE)

    – same initial positioning as above (inactive links high, active link low), but with some hover indication that each is a live link; I thought a basic greyscaled hover; the sprite already has grey versions of each image (

    I can’t tell you what a tremendous help you’ve been. As you saw from my first code, I know just enough to get myself in trouble. I’m learning a ton through this. Thanks!


    It’s my pleasure :) Glad I can help.

    I’ve created a new version. You will notice that if you turn off javascript, it works properly:

    You will also see what I’ve done. Basically the javascript removes the class. You can therefore use that class as anchor for browsers with disabled JS. I attach modernizr to pretty much every project I do and it does this by default.

    I also minimized the CSS a bit more. Remember, try to use the least amount of selectors you can. If ‘#nav a’ does the job, no need to go ‘#nav ul li a’


    div{display: block; height: 100px; width: 100px;}
    .one{background: red;}
    .two{background: blue;}
    .three{background: four;}

    is more efficient than:

    .one{background: red; display: block; height: 100px; width: 100px;}
    .two{background: blue; display: block; height: 100px; width: 100px;}
    .three{background: four; display: block; height: 100px; width: 100px;}

    Thank you, thank you, thank you. Both for helping with the code, but also for pointing out the inefficiency. That helps me tremendously.

    With and without JS enabled, with some easing effects, and with everything else dialed in, here it is: I’ll migrate it into WordPress tonight.

    The *only* think I noticed that was *slightly* off here was that the “nudging” for .current-menu-item works differently for about/faves/contacts than it does for home. Home is more abrupt, as thought it’s moving dramatically at first (like some positioning is off). It’s a small difference, but one I can’t figure out. It really only matters since “home” is the one that will most often be selected.

    Thanks again! (Where’s that “buy a beer” button in the forum….?)


    No problem! Glad you learnt something. There is no buy my a beer haha! But thanks ;)

    Yeah, it’s slighty longer than the other items (on the image), so it just required slightly more background-position-y than the others.


    Of course. I missed that. Thanks. Implemented the older version last night:

    It works well. There’s a latency, though. While the page loads, the menu acts like there’s no JS; then it kicks in. Now there is a fair bit of junk that I need to clean up to ease load time (hey, it’s a dev site right now), but is there a “trick” for front-loading the JS? It’s in my section right now.

Viewing 15 posts - 1 through 15 (of 17 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.