Skip to content

Instantly share code, notes, and snippets.

@kmgdevelopment
Created August 14, 2012 20:37
Show Gist options
  • Save kmgdevelopment/3352733 to your computer and use it in GitHub Desktop.
Save kmgdevelopment/3352733 to your computer and use it in GitHub Desktop.
Apply Effects to Ajax Function
$('a').click(function(e){
e.preventDefault();
var theUrl = $(this).attr('href');
$.ajax({
url: theUrl,
success: function(data){
var theTemplate = $(data);
$('.containerDiv').html(theTemplate);
}
});
});
@objectivehtml
Copy link

You could do something like this.

$('a').click(function(e){
e.preventDefault();
var theUrl = $(this).attr('href');
$.ajax({
url: theUrl,
success: function(data){
var theTemplate = $(data);
var duration = 500;

        $('.containerDiv').hide().html(theTemplate).fadeIn(duration);
    }
}); 

});

@objectivehtml
Copy link

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').hide().html(theTemplate).fadeIn(duration);
        }
    }); 
});

@objectivehtml
Copy link

This will fadeout your content.

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').fadeOut(duration, function() {
                $(this).hide().html(theTemplate).fadeIn(duration);
            });
        }
    }); 
});

@objectivehtml
Copy link

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').children().fadeOut(duration, function() {
                $('.containerDiv').children().html(theTemplate).fadeIn(duration);
            });
        }
    }); 
});

@objectivehtml
Copy link

You can add a selector to the children() method like so:

.children('.some-selector')

@kmgdevelopment
Copy link
Author

Looks good, with one adjustment:

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').children().fadeOut(duration, function() {
                $('.containerDiv').html(theTemplate).fadeIn(duration);
            });
        }
    }); 
});

The fadeIn doesn't appear to be working, though. The content appears, it just doesn't fade-in.

@objectivehtml
Copy link

Try this

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').children().fadeOut(duration, function() {
                $('.containerDiv').hide().html(theTemplate).fadeIn(duration);
            });
        }
    }); 
});

@kmgdevelopment
Copy link
Author

Yup, that works!

Why doesn't this work (the content is replaced but the fades don't work)?:

$('a').click(function(e){
    e.preventDefault();
    var theUrl = $(this).attr('href');
    $.ajax({
        url: theUrl,
        success: function(data){
            var theTemplate = $(data);
            var duration = 500;

            $('.containerDiv').children().fadeOut(duration);
            $('.containerDiv').html(theTemplate).fadeIn(duration);
        }
    }); 
});

@objectivehtml
Copy link

The fadeOut method is getting triggered, then immediately after, the fadeIn method is triggered. You need to use the fadeOut callback method so you trigger the fadeIn after the fadeOut is complete.

@kmgdevelopment
Copy link
Author

ah ok, that makes sense. The fadeIn starts after the previous event begins not ends. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment