python 分页居中算法
效果图:
代码:
python
def get_page_range(page_num, show_num, current_page):
# 分偶数和奇数
current_page = int(current_page)
page_num = int(page_num)
show_num = int(show_num)
if show_num % 2 == 0:
fore = int((show_num - 1) / 2)
after = int(show_num / 2)
else:
fore = int(show_num / 2)
after = math.ceil((show_num - 1) / 2)
star = current_page - fore
end = current_page + after
if star == 0:
star = 1
end += star
elif star < 0:
end += int(math.fabs(star))
end += 1
star = 1
if end > page_num:
star -= end - page_num
end = page_num
if star < 1:
star = 1
if end > page_num:
end = page_num
return star, end
def get_page_range(page_num, show_num, current_page):
# 分偶数和奇数
current_page = int(current_page)
page_num = int(page_num)
show_num = int(show_num)
if show_num % 2 == 0:
fore = int((show_num - 1) / 2)
after = int(show_num / 2)
else:
fore = int(show_num / 2)
after = math.ceil((show_num - 1) / 2)
star = current_page - fore
end = current_page + after
if star == 0:
star = 1
end += star
elif star < 0:
end += int(math.fabs(star))
end += 1
star = 1
if end > page_num:
star -= end - page_num
end = page_num
if star < 1:
star = 1
if end > page_num:
end = page_num
return star, end
根据上述代码传入对应的参数,然后就会返回页码的开始和结束。 在页面根据start和end 循环输出就可以了。