views:

6485

answers:

1

I would like to have two things happen at once when I click on a link. I am slowly going through Jquery documentation, but have yet to learn what I need yet.

Here is a link to my site

When I click on the services link I would Like the #slideshow div to hide, and a new div to replace it.

I had tried to just have the slideshow animate out on clicking the services link, but it would either do the animate function or go to the linked html page, not both. How would I accomplish both.

It doesn't matter if I just hide and show divs on the same page, or if I go to a new html page. I just want to have the animate function and change the content while hiding one.

this is the jquery code I am using

$(document).ready(function(){   
  $("a.home").toggle(
    function(){
      $("#slideshow").animate({ height: 'hide', opacity: 'hide' }, 'slow');   
    },
    function(){
      $("#slideshow").animate({ height: 'show', opacity: 'show' }, 'slow');   
    }
  );
});

$(document).ready(function(){   
  $("a.services").toggle(
    function(){
      $("#slideshow2").animate({ height: 'show', opacity: 'show' }, 'slow');
    },
    function(){
      $("#slideshow2").animate({ height: 'hide', opacity: 'hide' }, 'slow');
    }
  );
});

home and services are the two links, and I would like services when clicked to hide the #slideshow div, and show the slideshow2 div, which would be hidden and then replace the first one.

there is a fair amount of html so it may be easier to view my source from the link.

+2  A: 

Updated: This solution was updated following a follow-up question in the comments.

You should use the callback method of the show/hide methods.

$("a").click(function(){

  /* Hide First Div */
  $("#div1").hide("slow", function(){
    /* Replace First Div */
    $(this).replaceWith("<div>New Div!</div>");
  });

  /* Hide Second Div */
  $("#div2").hide("slow", function(){
    /* Replace Second Div */
    $(this).replaceWith("<div>New Div!</div>");
  });

  /* If you wanted to sequence these events, and only
     hide/replace the second once the first has finished,
     you would take the second hide/replace code-block 
     and run it within the callback method of the first
     hide/replace codeblock. */

});

The callback method is the second parameter of the .show/.hide functions. It is ran whenever the .show/.hide method is completed. Therefore, you can close/open your box, and within the callback method run an event immediately afterwards.

Jonathan Sampson
Great, that worked. Thanks!Just a quick question, how would I go about hiding two divs, and replacing them with two divs on a single click. Technically i get it to work, but it replaces both divs with the same thing, so it repeats. I updated my site with the current problem.Could you explain to me why, your code wont work without the previous code i was using below it.
Anders Kitson
The solution has been updated to demonstrate how you can hide two divs, and replace both with different divs.
Jonathan Sampson
Yes, now I remember all of this was covered in the tutorials in the jquery documentation, but I never grasp stuff until actually need to use it. Is there a way to animate the .hide up like slideup does instead of all into the top left corner
Anders Kitson
Just change "hide" to "slideUp" - if you check both effects in the documentation, you'll see that they have the same exact footprint: method(speed, [callback]); Meaning, all you have to do to change from hide to slideup, is change the name of the method :) That's it.
Jonathan Sampson
Your awesome! Two last questions, thanks so much by the way. Is there a way to animate the replaceWith and I am using a javascript font replace, http://wiki.github.com/sorccu/cufon/about is there a way to get that to apply to the divs that i replace it with, since they happen after it is run .
Anders Kitson
I'm not sure what you mean by "animate the replaceWith." That method strips out one peice of HTML, and inserts another. The only feeling of "animation" would be to fade the first one out, and fade in the second. But that isn't really animating the transition - it's just animating both items individually.
Jonathan Sampson
As for your second question, I've never used "Cufon," but if it has a manual method to invoke it, you would call that after adding your new divs. It appears, from my understanding of the doc, you can call Curfon.replace("element"); - which would likely be your option after the new divs are in view. Just change the H1 to reflect your new contents.
Jonathan Sampson