Last active
August 29, 2015 14:13
-
-
Save legalt/d11a70d5760d8d1ae82c to your computer and use it in GitHub Desktop.
pagination like rozetka.ua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var pages=[1,2,3,4,5,5,6,7,8,9,10,11,12,13,14,15]; | |
var current_index = 5; | |
function pagination(current_index,items){ | |
var arr=[]; | |
var temp = []; | |
var range = 2; | |
for (var i = 1; i<=items.length;i++){ | |
if(current_index>=1 && current_index<=4){ | |
if (i>(current_index+range)&&i!==items.length){ | |
arr.push('...'); | |
}else{ | |
arr.push(i); | |
} | |
}else if(current_index>4){ | |
console.log('b') | |
if (i!==1&&i<(current_index-range)||i>(current_index+range)&&i!==items.length){ | |
arr.push('...'); | |
}else{ | |
arr.push(i); | |
} | |
} | |
} | |
for (i=0;i<arr.length;i++){ | |
if (arr[i]!==arr[i+1]){ | |
if(arr[i]==='...'){ | |
temp.push({'id':i,link:0,title:'...',class:''}); | |
}else{ | |
if (i===(current_index-1)){ | |
temp.push({'id':i,link:arr[i],title:arr[i],class:'active'}); | |
}else{ | |
temp.push({'id':i,link:arr[i],title:arr[i],class:''}); | |
} | |
} | |
} | |
} | |
return temp; | |
} | |
console.log(pagination(current_index, pages)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment