I\'m using the loop + jQuery below to load in the next set of pages, and it works on the first click, but when the next page is loaded in and I click on \"newer posts\" it r
I used adeneo's solution, with a couple of minor tweaks.
.disabled
class.display: none;
once the opacity animation is complete). Instead, I manually animate the opacity, which limits the amount of height fluctuation.Here's the updated code:
$('#content').on('click', '#pagination :not(.disabled) a', function(e){
e.preventDefault();
var link = $(this).attr('href');
$.post( link, function( data ) {
var content = $(data).filter('#content');
var pagination = $(data).filter('#pagination');
$('#pagination').html(pagination.html());
$('#content').animate(
{opacity: 0},
500,
function(){
$(this).html(content.html()).animate(
{opacity: 1},
500
);
}
);
});
});
You're using jQuery's load()
method to insert content, which is a shortcut for $.ajax
, which of course inserts the content dynamically.
Dynamic content requires delegation of the events to a non-dynamic parent, something jQuery makes easy with on()
jQuery(function($) {
$('#content').on('click', '#pagination a', function(e){
e.preventDefault();
var link = $(this).attr('href');
$('#content').fadeOut(500, function(){
$(this).load(link + ' #content', function() {
$(this).fadeIn(500);
});
});
});
});