随着近几年前端行业的迅猛发展,各种层出不穷的新框架,新方法让我们有点眼花缭乱。
最近刚好比较清闲,所以没事准备撸撸前端的根基javascript,纯属练练手,写个分页,顺便跟大家分享一下
function pageFunc(conf){
this.myFunc = conf.click //用户点击要执行的方法
this.total = conf.total; //总页数
this.currentPage = 1; //当前页码
this.init() //初始化
}
pageFunc.prototype.init = function(){
var total = this.total,
currentPage = this.currentPage,
_this = this;
listeners = {
'setWhat' : function(opts) {
_this.aClick(opts.src)
return false;
}
};
listenersPre = {
'lmw-pre' : function(opts) {
_this.prevClick()
return false;
}
};
listenersAdd = {
'lmw-add' : function(opts) {
_this.addClick()
return false;
}
};
var rootele = this.createPage(1,total);
document.getElementById('page-coat').innerHTML = rootele
$on(document.getElementById('page-coat'), ['click'], listeners);//点击a标签
$on(document.getElementById('page-coat'), ['click'], listenersPre);//点击上一页
$on(document.getElementById('page-coat'), ['click'], listenersAdd);//点击下一页
}
//创建HTML分页结构字符串
pageFunc.prototype.createPage = function(page,total){
var str = `${page}`
for(var i=1;i<=3;i++){
if(page-i>1){
str = `${page-i}`+str
}
if(page+i1){
str = '...'+str
};
if(page+41){
str = `上一页1`+str
};
if(page
使用方法:
分页效果
#page-coat a{
text-decoration:none;
display: inline;
float: left;
padding: 3px 10px 3px 10px;
overflow: hidden;
border:1px solid #ccc;
color:#999;
margin-right: 5px;
cursor: pointer;
background: #fff;
}
#page-coat a:hover{
border: 1px solid #FF6600;
background-color: #FF6600;
color: #fff;
}
#page-coat span{
display: inline;
float: left;
color:#999;
background: #fff;
}
#page-coat a.lmw-current{
color: #FF6600;
border: 1px solid #FF6600;
background-color: #FFEEE5;
}
用原生还是比较麻烦的,为了实现业务,还得去封装一个模仿JQ的.on事件绑定方法。写的比较简陋,一些配置接口没有暴露出来,还可以再抽象暴露。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



