Skip to content

Instantly share code, notes, and snippets.

@daqi
Created July 19, 2016 11:01
Show Gist options
  • Save daqi/8dc0e47938628cd5d7e8ee18723083bc to your computer and use it in GitHub Desktop.
Save daqi/8dc0e47938628cd5d7e8ee18723083bc to your computer and use it in GitHub Desktop.
function navigation(curr, total, min) {
var left = 1;
var right = total;
var leftmin = Math.floor((min - 1) / 2);
var rightmin = (min - 1) % 2 === 0 ? leftmin : leftmin + 1;
var indexs = []
if (total >= min + 1) {
if (curr > leftmin && curr < total - rightmin) {
left = curr - leftmin
right = curr + rightmin
} else {
if (curr <= 5) {
left = 1
right = min
} else {
right = total
left = total - min + 1
}
}
}
while (left <= right) {
indexs.push(left)
left++
}
if (indexs.indexOf(1) == -1) {
if (indexs.indexOf(2) == -1) indexs.unshift('...')
indexs.unshift(1)
}
if (indexs.indexOf(total) == -1) {
if (indexs.indexOf(total-1) == -1) indexs.push('...')
indexs.push(total)
}
return indexs
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment