EZDC
EZDC

Reputation: 682

run animation then change page - jquery

I am trying to build a script that will prevent a link from changing pages immediately and running an animation first. This is what I have so far:

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

    $('#nav-bar').animate({
        left:'-260'
    }, 1500, function(){
//want to use callback function to load the page....
            $(this).attr('href') what next?

        }
    );

I would like to rebuild the default event and fire in the callback function. Is there an easy way to do this? THanks

Upvotes: 1

Views: 1545

Answers (2)

Gabriele Petrioli
Gabriele Petrioli

Reputation: 196002

Set the window.location to the href

$('a').click(function(event){
    event.preventDefault();
    var href = this.href;

    $('#nav-bar').animate({
        left:'-260'
    }, 1500, function(){
        //want to use callback function to load the page....
        window.location = href;
    }
);

Upvotes: 3

Musa
Musa

Reputation: 97672

Use window.location.href to navigate too the required page, also you'll have to save a reference to the link clicked so you can use it in the animate callback.

$('a').click(function(event){
    event.preventDefault();
    var self = this;
    $('#nav-bar').animate({
        left:'-260'
    }, 1500, function(){
            window.location.href = self.href;
        }
    );

Upvotes: 3

Related Questions