Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Infinites scroll jquery css fix WordPress

  • SOLVED

I have grid and list view toggle on my site www.starstyle.com
I use jquery to move the .entry-thumbnail div to the top of the article div in grid view and then back to its normal spot below the header text in list view in this js file
http://www.starstyle.com/wp-content/themes/starstyle/assets/js/custom.js
This works fine but when you click "show more" at the bottom of the page to show more posts
The infinite scroll doesnt have knowledge of this entry thumbnail div switch and im not sure what to add to the infinite scroll part of custom.js

Also wanted to kno if i should really be doing this in css or is jquery the best way

Answers (2)

2014-08-24

Remy answers:

You can do a call to your thumbnails() function when the infinite scroll is loading more posts :

$('#primary .content-inner').infinitescroll({
loading: {
finished: undefined,
finishedMsg: "",
msgText: "",
speed: 0,
img: 'http://www.starstyle.com/wp-content/themes/starstyle/images/loading.gif'
},
navSelector: "div.navigation-inner",
nextSelector: "div.navigation-inner a",
itemSelector: "#primary .content-inner .hentry",
donetext: "test",
errorCallback: function () {
$('div.navigation-inner').addClass('end');
$('div.navigation a').addClass('disabled').text('No More Posts');
}
}, function(newElements, data, url){
$('div.navigation-inner').css( 'display', 'block');
var i = $(this).find('.post').length;
i -= newElements.length - 1;
for( var key in newElements ) {
if ($('body.search,body.post-type-archive-product,body.child-category').size() > 0) {
$( newElements[key] ).removeClass('col-md-12').addClass('col-md-3 col-xs-6');
}
if ($('body.home, body.parent-category').size() > 0) {
if (getCookie('cat_listing') == 'grid'){
$( newElements[key] ).removeClass('col-md-12').addClass('col-md-3 col-xs-6');
}
}
i++;
}
thumbnails();
});


Katie comments:

I think its closer to this but still not working

if ($('body.home, body.parent-category').size() > 0) {

if (getCookie('cat_listing') == 'grid'){
thumbnails()
}

}
if ($('body.search, body.post-type-archive-product, body.child-category').size() > 0) {
thumbnails();
}

});


Remy comments:

Yes you're right, you need to do these checks since it's only for grid view. Did you have any error in your console when trying with this code ?


Katie comments:

I dont think there were any errors but it applied the thumbnails to all pics not just the newly loaded articles is $(newElements['key']).thumbnails(); correct syntax


Remy comments:

Like this then ? Did you get it to work ?

$('#primary .content-inner').infinitescroll({
loading: {
finished: undefined,
finishedMsg: "",
msgText: "",
speed: 0,
img: 'http://www.starstyle.com/wp-content/themes/starstyle/images/loading.gif'
},
navSelector: "div.navigation-inner",
nextSelector: "div.navigation-inner a",
itemSelector: "#primary .content-inner .hentry",
donetext: "test",
errorCallback: function () {
$('div.navigation-inner').addClass('end');
$('div.navigation a').addClass('disabled').text('No More Posts');
}
}, function(newElements, data, url){
$('div.navigation-inner').css( 'display', 'block');
var i = $(this).find('.post').length;
i -= newElements.length - 1;
for( var key in newElements ) {
if ($('body.search,body.post-type-archive-product,body.child-category').size() > 0) {
$( newElements[key] ).removeClass('col-md-12').addClass('col-md-3 col-xs-6');
}
if ($('body.home, body.parent-category').size() > 0) {
if (getCookie('cat_listing') == 'grid'){
$( newElements[key] ).removeClass('col-md-12').addClass('col-md-3 col-xs-6');
}

}
$(newElements['key']).thumbnails();
i++;
}
});


Katie comments:

no haven't gotten in yet - ok I cant do thumbnails(); because part of that function thumbnails() is already in there

its just this part I need I think



$('.entry-thumbnail').each(function(){
$closest = $(this).parent().parent().parent();
$closest.prepend($(this));
});


how can I add $( newElements[key] ) to the above code?


Remy comments:

Did you try this

thumbnail = $(newElements[key]).find( '.entry-thumbnail');
$closest = thumbnail.parent().parent().parent();
$closest.prepend(thumbnail);


Katie comments:

it worked!! thank you!

2014-08-25

Bob answers:

first take backup of your current js and
please try this js file as custom.js.

function newWindow(height, width) {
var settings = "height=" + height + ",width=" + width + ",status=yes,toolbar=no,menubar=no,location=no";
window.open("about:blank", "newwindow", settings);
}
jQuery(function($) {




$('#primary .content-inner').infinitescroll({
loading: {
finished: undefined,
finishedMsg: "",
msgText: "",
speed: 0,
img: 'http://www.starstyle.com/wp-content/themes/starstyle/images/loading.gif'
},
navSelector: "div.navigation-inner",
nextSelector: "div.navigation-inner a",
itemSelector: "#primary .content-inner .hentry",
donetext: "test",
errorCallback: function() {
$('div.navigation-inner').addClass('end');
$('div.navigation a').addClass('disabled').text('No More Posts');
}
}, function(newElements, data, url) {
for (var i = 0; i < newElements.length; i++) {
var thisElement = newElements[i];
// bind your event handlers here, e.g.:
$(thisElement).removeClass('col-md-12').addClass('col-md-3 col-xs-6');
var thumb =$(thisElement).find('.entry-thumbnail');
$closest = thumb.parent().parent().parent();
$closest.prepend(thumb);

//$(thisElement).mouseover(function(evt) { /* ... */ });
};
//thumbnails();
});
$(window).unbind('.infscr');
$('div.navigation-inner a').click(function() {
$('#primary .content-inner').infinitescroll('retrieve');
return false;
});
var scrollTimeout;
$('a.scroll-top').click(function() {
$('html,body').animate({
scrollTop: 0
}, 500);
return false;
});
$(window).scroll(function() {
clearTimeout(scrollTimeout);
if ($(window).scrollTop() > 400) {
scrollTimeout = setTimeout(function() {
$('a.scroll-top:hidden').fadeIn()
}, 100);
} else {
scrollTimeout = setTimeout(function() {
$('a.scroll-top:visible').fadeOut()
}, 100);
}
});

// Set cookie
function setCookie(c_name, value, exdays) {
var exdate = new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value = escape(value) + ((exdays == null) ? "" : "; expires=" + exdate.toUTCString());
document.cookie = c_name + "=" + c_value;
}

// Get cookie
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}

$('.post-layout a').click(function(e) {
e.preventDefault();
if ($(this).hasClass('active')) return
$this = $(this);
var layout = $this.attr('class');
var layout_final = $.trim(layout.split('-')[1]);

setCookie("cat_listing", layout_final, 365);
$('.row.main-content').fadeOut(function() {
$('.row.main-content').attr('class', 'row main-content').addClass($this.attr("class")).fadeIn();
if (layout_final == 'grid') {
thumbnails();
} else {
restore();
}
});

$('.post-layout a').removeClass('active');
$(this).addClass('active');


});


$('.nav .sub-menu-collapse').on('click', function(event) {
$(this).toggleClass('active');
});


function thumbnails() {

$('.entry-thumbnail').each(function() {
$closest = $(this).parent().parent().parent();
$closest.prepend($(this));
});
$('.hentry').removeClass('col-md-12').addClass('col-md-3 col-xs-6');
$('#primary.site-content').removeClass('col-md-8').addClass('col-md-12');


}

function restore() {
$('article').each(function() {
$thumb = $(this).find('.entry-thumbnail');
$(this).find('.featured').prepend($thumb);
});
$('.hentry').removeClass('col-md-3 col-xs-6 ').addClass('col-md-12');
$('#primary.site-content').removeClass('col-md-12').addClass('col-md-8');


}
$(function() {
$("[data-toggle='tooltip']").tooltip();
});

/*

if ($('body.home, body.parent-category').size() > 0) {

if (getCookie('cat_listing') == 'grid'){
thumbnails();

}

}
*/
if ($('body.search, body.post-type-archive-product, body.child-category').size() > 0) {
thumbnails();
}

});


Bob comments:

the function which execute after loading new content should have this code


//loop through all element
for (var i = 0; i < newElements.length; i++) {
var thisElement = newElements[i]; // create object
// bind your event handlers here, e.g.:
$(thisElement).removeClass('col-md-12').addClass('col-md-3 col-xs-6'); // change css classes
var thumb =$(thisElement).find('.entry-thumbnail'); // create thumb object
$closest = thumb.parent().parent().parent(); // find place to prepand
$closest.prepend(thumb); // prepand old created thumb

}