Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save CezaryDanielNowak/8721979 to your computer and use it in GitHub Desktop.
Save CezaryDanielNowak/8721979 to your computer and use it in GitHub Desktop.
// I'ts ugly but works
$('.pagination').each(function(){
var allLi = $(this).find('li');
if(allLi.length > 20) {
var activeId = allLi.filter('.active').index();
allLi.eq(0)
.add(allLi.eq(1))
.add(allLi.eq(2))
.add(allLi.eq(3))
.add(allLi.eq(4))
.add(allLi.eq(-1))
.add(allLi.eq(-2))
.add(allLi.eq(-3))
.add(allLi.eq(-4))
.add(allLi.eq(activeId))
.add(allLi.eq(activeId-1))
.add(allLi.eq(activeId-2))
.add(allLi.eq(activeId-3))
.add(allLi.eq(activeId+1))
.add(allLi.eq(activeId+2))
.add(allLi.eq(activeId+3))
.addClass('allow');
var replacedWithDots = false;
allLi.each(function() {
if( $(this).hasClass('allow') ) {
replacedWithDots = false;
} else if(!replacedWithDots) {
replacedWithDots = true;
$(this).html('<a>...</a>');
} else {
$(this).remove();
}
})
}
});
@CezaryDanielNowak
Copy link
Author

I wrote this 6 years ago, and code isn't good :D

But in general:

  • take 4 list element from the beginning
  • take 4 list element from the end
  • take 3 elements around active one
  • replace all other elements with ...

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