window.page =  function page(ele, para) {
this.ele = document.querySelector(ele); this.options = {
count: 100,
pageSize: 8,
showPage: 5,
prevContent:"<上一页",
nextContent:">下一页",
currentPage:1,
callBack:function () { }
}
this.extend(para);
this.currentPage = this.options.currentPage;
this.totalPage = Math.ceil(this.options.count / this.options.pageSize);
this.sideNum = Math.floor(this.options.showPage / 2);
this.create();
} page.prototype.extend = function(para) {
for(var i in para) {
this.options[i] = para[i];
}
} page.prototype.create = function() {
var that = this; this.prev = document.createElement("div");
this.prev.className = "prev";
this.prev.innerHTML = this.options.prevContent;
this.ele.appendChild(this.prev); this.content = document.createElement("ul");
this.content.className = "middle"; this.ele.appendChild(this.content); this.next = document.createElement("div");
this.next.className = "next";
this.next.innerHTML = this.options.nextContent;
this.ele.appendChild(this.next);
this.createPage();
//上一页的点击事件
this.prev.onclick = function() {
that.currentPage--;
if(that.currentPage < 1) {
that.currentPage = 1;
}
if(this.className == "prev"){
that.createPage();
}
}
//下一页的点击事件
this.next.onclick = function() {
that.currentPage++;
if(that.currentPage > that.totalPage) {
that.currentPage = that.totalPage;
}
if(this.className=="next"){
that.createPage();
} }
} page.prototype.createPage = function() {
var that = this; that.options.callBack(that.currentPage,that.options.pageSize);
this.content.innerHTML = "";
var offsetSize = this.getOffetSize();
this.prev.className = "prev";
this.next.className = "next";
if(that.currentPage == 1){
//第一个
this.prev.className = "prev disabled";
}
if(that.currentPage >= this.totalPage){
this.next.className = "next disabled";
} for(var i = offsetSize.start; i <= offsetSize.end; i++) {
var li = document.createElement("li");
li.innerHTML = i;
if(i == that.currentPage) {
li.className = "active";
}
this.content.appendChild(li);
li.onclick = function() {
that.currentPage = +this.innerHTML;
that.createPage();
}
}
} page.prototype.getOffetSize = function() {
var start = 1;
var end = this.options.showPage;
if(this.currentPage >= this.totalPage){
this.currentPage = this.totalPage;
}
if(this.totalPage < this.options.showPage) {
return {
start: 1,
end: this.totalPage
}
}
start = this.currentPage - this.sideNum;
end = this.currentPage + this.sideNum;
if(start < 1) {
start = 1;
end = this.options.showPage;
}
if(end > this.totalPage) {
end = this.totalPage;
start = this.totalPage - 2 * this.sideNum;
}
return {
start: start,
end: end
}
}

  

function newPage(opt) {
var pageCom = new page(".page", {
count: opt.count,
currentPage:opt.currentPage,
pageSize: pageSize,
showPage: 5,
prevContent: "<上一页",
nextContent: ">下一页",
callBack:function (index,size) {
console.log(index,size); }
});
}

  

 ._page{
width: 100%;
height: 30px;
padding:20px 0;
margin-top: 30px;
margin-bottom: 40px;
text-align: right;
} ._page .prev,
._page .next {
display: inline-block;
width: 80px;
height: 30px;
background: #3385ff;
line-height: 30px;
text-align: center;
color: #fff;
vertical-align: middle;
cursor: pointer;
}
._page .disabled{
cursor: not-allowed;
background: #ccc;
} ._page .next {
margin-left: 4px;
} ._page .middle {
list-style: none;
display: inline-block;
vertical-align: middle;
} ._page .middle li {
width: 30px;
height: 30px;
background: #666;
line-height: 30px;
text-align: center;
color: #fff;
margin-left: 4px;
float: left;
} ._page .middle li.active {
background: limegreen;
}

  

原生js分页器插件的更多相关文章

  1. 使用 原生js 制作插件 (javaScript音乐播放器)

    1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  2. 原生Js弹窗插件|web弹出层组件|对话框

    wcPop.js 是一款基于原生javascript开发的前端 web版 弹窗组件,遵循原生 H5/css3/JS 的书写规范,简单实用.拿来即用(压缩后仅10KB).已经兼容各大主流浏览器.内含多种 ...

  3. js分页器插件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  4. (三)原生JS实现 - 插件 - 弹出层

    创建遮罩层 _createCover: function() { var newMask = document.createElement("div"); newMask.id = ...

  5. 原生js封装插件

    https://www.jianshu.com/p/937c6003851a object-fit: cover:https://www.jianshu.com/p/a2ce70fa9584 flex ...

  6. 原生js实现单屏滚动

    类似于fullpage的单屏滚动,使用原生JS实现,不依赖任何js库: css: html,body {height:100%;} body {margin:0px;} div {height:100 ...

  7. 原生JS封装Ajax插件(同域&&jsonp跨域)

    抛出一个问题,其实所谓的熟悉原生JS,怎样的程度才是熟悉呢? 最近都在做原生JS熟悉的练习... 用原生Js封装了一个Ajax插件,引入一般的项目,传传数据,感觉还是可行的...简单说说思路,如有不正 ...

  8. 原生JS实现"旋转木马"效果的图片轮播插件

    一.写在最前面 最近都忙一些杂七杂八的事情,复习软考.研读经典...好像都好久没写过博客了... 我自己写过三个图片轮播,一个是简单的原生JS实现的,没有什么动画效果的,一个是结合JQuery实现的, ...

  9. 原生js实现autocomplete插件

    在实际的项目中,能用别人写好的插件实现相关功能是最好不过,为了节约时间成本,因为有的项目比较紧急,没充分时间让你自己来写,即便写了,你还要花大量时间调试兼容性.但是出于学习的目的,你可以利用闲暇时间, ...

随机推荐

  1. 【转】Python基础-字符串

    原文地址http://blog.chinaunix.net/uid-21169302-id-446256.html Python-String-Function 字符串中字符大小写的变换: * S.l ...

  2. 认识wsgi

    WSGI是什么? WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义 ...

  3. CMMI基础知识扫盲

    CMMI全称是Capability Maturity Model Integration,CMMI是个好东西来的,但行内人士对她的认识并不全面,甚至有种种的误解.尽管网上有很多CMMI相关介绍,但一般 ...

  4. android: requestLayout(), invalidate(), postInvalidate() 方法区别

    一.invalidate和postInvalidate 这两个方法都是在重绘当前控件的时候调用的.invalidate在UI线程中调用,postInvalidate在非UI线程中调用.因为androi ...

  5. LC 450. Delete Node in a BST

    Given a root node reference of a BST and a key, delete the node with the given key in the BST. Retur ...

  6. VCTravel

    #pragma once #include <osgViewer/Viewer> #include <osgViewer/ViewerEventHandlers> #inclu ...

  7. Sass(Syntactically Awesome Stylesheets)——概述(待续)

    官网地址:http://sass.bootcss.com/ Sass(Syntactically Awesome Stylesheets) Sass 是成熟.稳定.强大的 CSS 扩展语言. 特征 兼 ...

  8. 转载:Spark中文指南(入门篇)-Spark编程模型(一)

    原文:https://www.cnblogs.com/miqi1992/p/5621268.html 前言 本章将对Spark做一个简单的介绍,更多教程请参考:Spark教程 本章知识点概括 Apac ...

  9. elk收集tomcat日志

    1.elk收集tomcat普通日志: 只在logstash节点增加如下文件,重启logstash即可: cat >>/home/logstash-6.3.0/config/tomcat_t ...

  10. Mysql开启审计功能

    第一种经验证,有效. 第一种用macfee的mysql审计插件. 下载地址:https://bintray.com/mcafee/mysql-audit-plugin/release/1.1.4-72 ...