views:

40

answers:

2

I'm trying to use .fadeIn() to make my navigational hover effects look a little more smooth on the transition. Except I'm getting what I can only describe as a double fade (in and out and back in again).

I'm brand new to JS and the jQuery API so any help appreciated. I'm an old pro with CSS, so I still think in those terms. On this one I'm adding a class to switch the background sprite bg-position down. Is the problem with my jQuery, my CSS or both?

http://tuscaroratackle.com is the instance in question - the nav links.

+4  A: 

Instead of mouseout and mouseover which fire even when entering a child, you can use .hover(), like this:

$("#nav li").hover(function(){
  $(this).find("a").addClass("hover").fadeIn();
}, function(){
  $(this).find("a").removeClass("hover").fadeOut();
});

.hover() maps to the mouseenter and mouseleave events which don't fire when entering/leaving children, which is what's causing the double animation in your current code.


Not directly related to the question but there are some other issues on the page you'll want to address, jQuery is included (the latest 1.4.x) then jQuery 1.2.6 is included later with version 1.5.1 of the validation plugin (which is up to 1.7 now). I'd look at upgrading your validation plugin and removing the jQuery 1.2.6 include, as it's likely to cause you headaches later (and a heavier page for users now).

Nick Craver
hmm - I tried it your way but now I'm not getting any animation. Do I have a syntax error or something?
JAG2007
@JAG2007 - I did have an extra brace in there, try the current answer :)
Nick Craver
Yeah - that works now. Only I'm not getting the fadeOut working properly. When I move my mouse away instead it's just disappearing w/o a fadeOut. Do you think that's actually a problem with how I have the CSS written for the effect?
JAG2007
Also, not seeing where I have jQuery included twice.
JAG2007
@JAG2007 - This laptop's giving me issues at the moment, but it *looks* like yes the `.hover` class is the culprit, try this instead in the second function: `$(this).find("a").fadeOut(function() { $(this).removeClass("hover"); });`
Nick Craver
@JAG2007 - Line 15 == jQuery 1.4.x, Line 49 == jQuery 1.2.6
Nick Craver
OK, all is well now. And good catch on the jQuery version. The one is being called as part of my tempwarning banner - so I didn't see it immediately from the index.php, plus that link was part of the MailChimp form, so I didn't realize I could remove it.
JAG2007
Hey here's a question for you - why when I removed the line 49 jQuery statement did the script fail? Is there something wrong with the way I have the line 15 script written?
JAG2007
At the moment it looks like it is working in FF3 but chrome5 is failing. Will take a look at the code.
Josh Stuart
Yeah - I'm not getting it in FF 3.6.4 either.
JAG2007
Yeah - odly enough all is well whenever I add that second jQuery library back in at line 49.
JAG2007
@JAG2007 - Did you update the validation plugin like in the answer? :) The 2 versions of jQuery are very different, you'll need to grab a later version of the plugin, 1.7 at the moment.
Nick Craver
No - I don't understand that part. I'm not familiar with the validation plugin. I don't even know what it is!
JAG2007
+1  A: 

you may want to do it this way,

$("#nav li").hover(function(){    
    $(this).find("a").fadeIn();
},
function(){
    $(this).find("a").fadeOut();
}).find("a").addClass("hover").hide();

I hide a on it on view, it will then be showed on hover.

you can also set display:none for hover class so that you don't have to call .hide() in there.

here's a demo

Reigel
While it is a solution, it doesn't do anything to answer the question: "Is the problem with my jQuery, my CSS or both?" or elaborating on any part of that :)
Nick Craver
hehe ;) yeah...
Reigel
can you double check your syntax on that? It's producing two errors in Firefox, unless I just copied it wrong.
JAG2007
nm - turned out to be something else.
JAG2007
Even though you didn't answer the question fully, I like your solution best. It's a little less buggy than Nick's. Thanks Reigel!
JAG2007