views:

128

answers:

2

I have this code where I am using jQuery to navigate to the next page, because I want some effects to take place before that happens. The problem is, that everything after the prevent.Default(); doesn't seem to work!

$("a").click(function(event){
    event.preventDefault();
    $(".content-center").animate({height: "0px"}, 500);
    navigate($(this).attr('href'));
});

I need things to happen in that order, so that the animation happens and once it's complete - load the next page...

Does anyone have any ideas? Many thanks in advance?

Tim


Updated code (moves to new page but no animation occurs) ---

$("a").click(function(event){
    event.preventDefault();

    var driver = $(this).attr('href');
    $(".content-center").animate({
        height: "0px"
    }, 500, function(){
        navigate(driver);
    });
});

see: http://bit.ly/aOeYgE

Many thanks for your help!!

+2  A: 

You need to put the navigate method call in the the animation callback.

$("a").click(function(event){
    event.preventDefault();

    var $a = $(this);
    $(".content-center").animate({
        height: "0px"
    }, 500, function(){
        navigate($a.attr('href'));
    });
});

If you don't do this, the animation and the navigation will happen at the same time.

Rich

kim3er
Sorry I'm still getting used to how stackoverflow works! Please see above!
Tim
You have an extra '});' between the click and hover event handlers.
kim3er
Ah so I do! I've taken that out now though, and the animation works, but not the navigate! Argh!! Any ideas? Thanks for your help
Tim
I'm not sure what navigate is, I'd use location.href = ($a.attr('href');
kim3er
A: 

Try using the callback function parameter of the animate function to implement navigation once the animation is complete (from the hip):

$("a").click(function() { 
  var driver = this;
  $(".content-center").animate({height: "0px"}, 
                               500,
                               function() { navigate($(driver).attr('href')); }
                              ); 

});
BradBrening
Your example is the same as mine, but you've forgotten to prevent the default behaviour of the click, which will override the animation.
kim3er