当前位置: 首页 > Web前端 > HTML

分页模块

时间:2023-04-02 16:50:49 HTML

1.功能描述先贴最终效果图:第一页/最后一页/上一页/下一页的功能我就不用详细解释了:)中间固定了11个页码(这个数字是一个下面代码中常量DISPLAY_NUM),根据点击的页码a,页面变化分为三种:(1)1<=a<=5,显示效果如上图所示。(2)6<=a<=l-4,其中l代表总页数,效果如下:(3)l-4<=a<=l,效果如下:第二段代码html部分

js部分define([],function(){vartpl='<主页\上一页\{{#total_arr}}\{{num}}\{{/total_arr}}\下一页>
\尾页>
',default_event_handlers={to_page:function(e,cur){returntrue;},prev_page:function(e,cur){返回真;},next_page:function(e,cur){返回真;},first_page:function(e,cur){返回真;},last_page:function(e,cur){返回真;}},DISPLAY_NUM=11,pages={/****@param{type}opt*{*wrp*that:event_handlers函数调用上下文,默认情况下为this*events:{"to_page":function(event,cur){},"prev_page":function(evnet,cur){},"next_page":function(event,cur){},"first_page":function(event,cur){},"last_page":function(event,cur){},}*}*@returns{undefined}*/init:function(opt){varevent_handlers=$.extend复制代码({},default_event_handlers,opt.events),_this=opt.that||this,_that=this;$(opt.wrp).on('click','.pages.prev-page',function(e){varbefore_p=$(this).closest('.pages').find('.cur').data("value"),total_p=$(this).siblings('.last-page').data('value');before_p=parseInt(before_p);total_p=parseInt(total_p);if(before_p==1){return;}varcur=before_p-1;event_handlers.prev_page.call(_this,e,cur);_that.render(cur,total_p);}).on('click','.pages.next-页面',功能(e){varbefore_p=$(this).closest('.pages').find('.cur').data("value"),total_p=$(this).next().data("value");before_p=parseInt(before_p);total_p=parseInt(total_p);如果(before_p==total_p){返回;}varcur=before_p+1;event_handlers.next_page.call(_this,e,cur);_that.render(cur,total_p);}).on('click','.pages.num',function(e){varbefore_p=$(this).closest('.pages').find('.cur').data("value"),total_p=$(this).siblings('.last-page').data('value'),cur=$(this).data("value");before_p=parseInt(before_p);total_p=parseInt(total_p);cur=parseInt(cur);如果(before_p==cur){return;}event_handlers.to_page.call(_this,e,cur);_that.render(cur,total_p);}).on('click','.pages.first-page',function(e){varbefore_p=$(this).closest('.pages').find('.cur').data("值"),total_p=$(this).siblings('.last-page').data('value');before_p=parseInt(before_p);total_p=parseInt(total_p);if(before_p==1){返回;}varcur=1;event_handlers.first_page.call(_this,e,cur);_that.render(cur,total_p);}).on('click','.pages.last-page',function(e){varbefore_p=$(this).closest('.pages').find('.cur').data("value"),total_p=$(this).data("value");before_p=parseInt(before_p);total_p=parseInt(total_p);if(before_p==total_p){返回名词;}varcur=total_p;event_handlers.last_page.call(_this,e,cur);_that.render(cur,total_p);});},/****@param{type}opt*total*cur*@returns{undefined}*/render:function(cur,total){if(total==0){$('.pages').html('');返回;}vartotal_arr=this.set_total_array(total,cur),prev_d=(cur==1)?'禁用':'',next_d=(cur==total)?'禁用':'',content=Mustache.render(tpl,{prev_d:prev_d,next_d:next_d,total_value:total,total_arr:total_arr});$('.pages').html(内容);},set_total_array:function(total,current){vartotal_arr=[],begin_index=1,end_index=total,begin_threshold=Math.floor(DISPLAY_NUM/2),end_threshold=total+1-begin_threshold;如果(总计<=DISPLAY_NUM){end_index=总计;}elseif(current<=begin_threshold){end_index=DISPLAY_NUM;}elseif(current>=end_threshold){begin_index=total+1-DISPLAY_NUM;}else{begin_index=current-begin_threshold;end_index=当前+begin_threshold;}for(vari=begin_index;i<=end_index;i++){varitem={num:i,value:i};if(current==i){item.cur="cur";}total_arr.push(item);}返回total_arr;},};返回页面;});csssection.pages{text-align:center;填充:20px0;.prev,.next{宽度:自动;背景:透明;-moz-box-sizing:边框框;-webkit-box-sizing:border-box;框大小:边框框;}a{显示:内联块;宽度:18px;高度:18px;行高:18px;边界半径:3px;文本对齐:居中;颜色:#333;字体大小:14px;垂直对齐:顶部;右边距:15px;}.cur{背景颜色:#3b9bf5;白颜色;文字修饰:无;游标:默认;;}.clear-margin{margin-left:-15px;}.disabled{不透明度:0.6;}}3使用示例模板页面控制器先初始化:再渲染:init和render函数的参数见js代码部分注释