Home › Forums › JavaScript › Showing/Hiding Lists › Re: Showing/Hiding Lists
September 5, 2011 at 2:21 am
#86394
Member
hey!
check the cor below! its working with less and more both.
i have added one constant variable visible which will set how many are visible at once!
link to jsfiddle:
jQuery(function($) {
var visible = 7;
$('ul li:gt('+(visible - 1)+')').hide();
$('#more').click(function() {
if ($('ul li:visible:last').is(':last-child')) {
return false;
}
var currentIndex = $('ul').children('li:visible:last').index(),
nextIndex = currentIndex + (visible + 1);
$('ul li').hide();
$('ul li:lt(' + nextIndex + '):gt(' + currentIndex + ')').show();
});
$('#less').click(function() {
if ($('ul li:visible:first').is(':first-child')) {
return false;
}
var currentIndex = $('ul').children('li:visible:first').index();
var prevIndex = (currentIndex - (visible + 1));
if(prevIndex < 0) {
prevIndex = 0;
}
$('ul li').hide();
if(prevIndex == 0)
$('ul li:lt(' + currentIndex + ')').show();
else
$('ul li:lt(' + currentIndex + '):gt(' + prevIndex + ')').show();
});
});