Skip to content

Instantly share code, notes, and snippets.

@raidenz
Created October 25, 2025 05:28
Show Gist options
  • Select an option

  • Save raidenz/c53115043a5c2eb4ee9174b35bc452e2 to your computer and use it in GitHub Desktop.

Select an option

Save raidenz/c53115043a5c2eb4ee9174b35bc452e2 to your computer and use it in GitHub Desktop.
elipsis pagination
// make pagination array smaller, better for responsive design
function elipsisPagination(currentPage, totalPages, visibleCount = 5) {
const pages = []
const showEllipsis = (arr, position) => {
if (position === 'start' && arr[0] !== 1) {
arr.unshift('...')
arr.unshift(1)
}
if (position === 'end' && arr[arr.length - 1] !== totalPages) {
arr.push('...')
arr.push(totalPages)
}
return arr
}
const half = Math.floor(visibleCount / 2)
let start = Math.max(currentPage - half, 1)
let end = Math.min(currentPage + half, totalPages)
// Adjust if near the start or end
if (currentPage <= half) {
start = 1
end = visibleCount
} else if (currentPage + half >= totalPages) {
start = totalPages - visibleCount + 1
end = totalPages
}
for (let i = start; i <= end; i++) {
pages.push(i)
}
return showEllipsis(showEllipsis(pages, 'start'), 'end')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment