tags:

views:

74

answers:

3

I have 2 #header elements with different info in each that I want to fade in/out on a hover event.

I can do the fade in/out no problem BUT I really want the elements to be hidden, i.e. using "display:none;" AFTER they have faded out and THEN bring them back once they fade in.

I have this already for the fade:

$(document).ready(function()
{ 

  $("#header").hover
   (function()
   {
    $(this).fadeTo("fast", 0);
     $('#header_hover').fadeTo("fast", 1.0);    
   },
   function()
   {
    $(this).fadeTo("fast", 1.0); 
     $('#header_hover').fadeTo("fast", 0); '5'});      

   });

 });

Which works great but although this changes the 'opacity' of each item they still exist together in code, so its as if I have 2 headers which I don't want.

FYI I'm using this CSS to show one header behind the other:

    #header, #header_hover
{
 height:260px;
 padding:0;
 margin:20px 10px;
 position:absolute;
 top:20px;
}

#header
{
 background:red;
 z-index:10;
}
#header_hover
{
 background:blue; 
 z-index:5;
}

I've tried a number of different solutions in jQuery, nothing has worked. At worst I get a crazy look at best one shows but the other is hidden forever?

A: 

Use the animate function:

$(this).animate({opacity: 'toggle'}, 'fast');

Or the fadeIn fadeOut function:

$(this).fadeOut('fast');

something like this:

$(document).ready(function()
{ 

  $("#header").hover
   (function()
   {
    $(this).fadeOut("fast");
     $('#header_hover').fadeIn("fast");    
   },
   function()
   {
    $(this).fadeIn("fast"); 
     $('#header_hover').fadeOut("fast"); '5'});      

   });

 });
RJD22
A: 
$(element).fadeOut("slow").hide();

AFAIK the execution flow will prevent "hide" execution before "fadeOut" ends.

migajek
fadeout doesn't need a hide function because its build in
RJD22
A: 

You just have to show #header_hover before fading in (normally), and hide #header after fading out (using a callback).

$("#header").hover(function() {
    $(this).fadeTo("fast", 0);
    $('#header_hover').show();
    $('#header_hover').fadeTo("fast", 1.0);    
}, function() {
    $(this).fadeTo("fast", 1.0);
    $('#header_hover').fadeTo("fast", 0, function() {
        $(this).hide();
    });               
});

Try it here (Note: The red div has less content, so you can see the effect.).

melhosseiny
You are doing the same as the function fadein and fadeout
RJD22
Did you look at his code? I'm just modifying his code to give him the functionality he needs. I'd use fadeIn and fadeOut, but that's not the point of his question.
melhosseiny
It is because as his code clearly states he just wants to fade in and fade you since he is fading from 0(invisible) to 1.0(completely visible). So why not give him best practice.
RJD22
Yes, I agree. If he wants to go all the way, he should use fadeIn/fadeOut. I just assumed he was asking a general fadeTo question.
melhosseiny
Guys, I tried fadeIn/fadeOut and it didn't work in the same way. I've had experience with 'fadeTo' in the past so stuck with that.Anyhow, I've ended up with a slightly different solution to the options laid out here but this answer is the best fix for what I asked. Thanks all round.
mkjones