Skip to content

Instantly share code, notes, and snippets.

@imzhi
Last active August 29, 2015 14:22
Show Gist options
  • Save imzhi/fd6fc6657a5d2ea8125c to your computer and use it in GitHub Desktop.
Save imzhi/fd6fc6657a5d2ea8125c to your computer and use it in GitHub Desktop.
js 分页。类似这种(前展示3页,后展示3页),5 6 7 [8] 9 10 11
/**
* 页码显示插件
* @author imzhi
* @date 2015-06-01
*
* @param total 总条数
* @param per 每页条数
* @param current 当前页码
* @return {string} str 返回页码字串
*/
function pagebar(total, per, current) {
var side = 3; // 两边分别展示3页
var start = 1;
var end = Math.ceil(total / per);
var s_dis = current - start;
var e_dis = end - current;
var s_page = current - side < 1 ? 1 : current - side,
e_page = current + side > end ? end : current + side;
var i;
var arr = [];
if (end > 2 * side + 1) {
if (s_dis < side) {
e_page = side - s_dis + current + side > end ? end : side - s_dis + current + side;
} else if (e_dis < side) {
s_page = current - side - side + e_dis < 1 ? 1 : current - side - side + e_dis;
}
}
for (i = s_page; i <= e_page; i++) {
arr.push(i);
}
return arr.join(",");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment