原生js分页器插件
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分页器插件的更多相关文章
- 使用 原生js 制作插件 (javaScript音乐播放器)
1.引用页面 index.html <!DOCTYPE html> <html lang="en"> <head> <meta chars ...
- 原生Js弹窗插件|web弹出层组件|对话框
wcPop.js 是一款基于原生javascript开发的前端 web版 弹窗组件,遵循原生 H5/css3/JS 的书写规范,简单实用.拿来即用(压缩后仅10KB).已经兼容各大主流浏览器.内含多种 ...
- js分页器插件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- (三)原生JS实现 - 插件 - 弹出层
创建遮罩层 _createCover: function() { var newMask = document.createElement("div"); newMask.id = ...
- 原生js封装插件
https://www.jianshu.com/p/937c6003851a object-fit: cover:https://www.jianshu.com/p/a2ce70fa9584 flex ...
- 原生js实现单屏滚动
类似于fullpage的单屏滚动,使用原生JS实现,不依赖任何js库: css: html,body {height:100%;} body {margin:0px;} div {height:100 ...
- 原生JS封装Ajax插件(同域&&jsonp跨域)
抛出一个问题,其实所谓的熟悉原生JS,怎样的程度才是熟悉呢? 最近都在做原生JS熟悉的练习... 用原生Js封装了一个Ajax插件,引入一般的项目,传传数据,感觉还是可行的...简单说说思路,如有不正 ...
- 原生JS实现"旋转木马"效果的图片轮播插件
一.写在最前面 最近都忙一些杂七杂八的事情,复习软考.研读经典...好像都好久没写过博客了... 我自己写过三个图片轮播,一个是简单的原生JS实现的,没有什么动画效果的,一个是结合JQuery实现的, ...
- 原生js实现autocomplete插件
在实际的项目中,能用别人写好的插件实现相关功能是最好不过,为了节约时间成本,因为有的项目比较紧急,没充分时间让你自己来写,即便写了,你还要花大量时间调试兼容性.但是出于学习的目的,你可以利用闲暇时间, ...
随机推荐
- chrome-添加JSON-handler插件
1.访问http://jsonhandle.sinaapp.com/下载 2.谷歌访问 chrome://extensions/ 然后找到你下载的JSON-handle_0.5.2.crx文件,直 ...
- TortoiseSVN 使用教程
TortoiseSVN 使用教程 TortoiseSVN 是 Subversion 版本控制系统的一个免费开源客户端,可以超越时间的管理文件和目录. TortoiseSVN 安装 下载地址:https ...
- html,body设置{height:100%}[转]
一般情况下,我们css控制的最高节点就是body,例如设置: body{background:#069;} 则浏览器界面就是完全的#068的背景色.这里看上去是<body>标签下的背景色起 ...
- Facebook libra开发者文档- 1 -welcome
参考https://developers.libra.org/docs/welcome-to-libra 欢迎来到Libra开发者网站!Libra的使命是建立一个简单的全球货币和金融基础设施,为数十亿 ...
- Linux交换空间(swap space)的那些优缺点
下面的所有例子都在ubuntu-server-x86_64 16.04下执行通过 什么是swap? swap space是磁盘上的一块区域,可以是一个分区,也可以是一个文件,或者是他们的组合.简单点说 ...
- linux ubuntu 如何解决warning: no newline at end of file?
今天写了一段代码, 是在Windows下编辑的, 保存后放在linux系统下编译. gcc和cc都产生以下的警告: a.h:1:2: warning: no newline at end of fil ...
- c# 遍历局域网计算机(电脑)获取IP和计算机名称
c#可以遍历局域网计算机,获取全部计算机的名称和IP地址,网上提供了相关的几种方法,并对效率进行了比较,但是没有对各种方法进行比较,以确定可以使用的情况.这篇文章将对这几种方法进行分析,以帮助了解各种 ...
- springboot拦截json后缀的请求,返回json数据
需求:请求list.json返回以下数据 { "jsonResult": { "code": 200, "message": "查 ...
- (十八)Centos之firewall 防火墙命令
如果你的系统上没有安装使用命令安装 #yum install firewalld //安装firewalld 防火墙 开启服务 # systemctl start firewalld.service ...
- expect自动登录
.安装软件 yum install expect -y .查看expect命令位置 expect命令的位置也可能是在 /usr/local/bin/expect,使用前用户应确认工具是否存在(确认方法 ...