原生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插件
在实际的项目中,能用别人写好的插件实现相关功能是最好不过,为了节约时间成本,因为有的项目比较紧急,没充分时间让你自己来写,即便写了,你还要花大量时间调试兼容性.但是出于学习的目的,你可以利用闲暇时间, ...
随机推荐
- js type
js中的数据类型有undefined,boolean,number,string,object等5种,前4种为原始类型,第5种为引用类型. new Object() functionundefined ...
- equal numbers
给你一个n长度的数组,让你修改0到n次,问每次修改后能剩下不同个数的最小数是多少: 这里有了两种做法,一种是变成他们的lcm这样的话,修改后答案应该是减去改过的个数然后在加一 另一种就是数字修改成序列 ...
- qt 关于Qt中MVC的介绍与使用
Qt包含一组使用模型/视图结构的类,可以用来管理数据并呈现给用户.这种体系结构引入的分离使开发人员更灵活地定制项目,并且提供了一个标准模型的接口,以允许广泛范围的数据源被使用到到现有的视图中. 模型 ...
- maven install的时候把源码一起放到仓库
在pom.xml中加入插件 <build> <plugins> <!-- Source attach plugin --> < ...
- Ionic 的安装运行
1.学习前准备工作 1.必须得安装 nodejs (建议安装最新的稳定版本) 2.必须有 Angular 基础:https://www.loaderman.com/goods-1047.html 2. ...
- opencv之dlib库人脸识别
基础知识 python知识: import os,shutil shutil.rmtree("C:\\Users\\yangwj\\Desktop\\test") #删除目录 os ...
- HTML5 地理位置定位API(5)
HTML5 Geolocation API (地理位置应用程序接口) 目前PC浏览器支持情况: Firefox 3.5+Chrome 5.0+Safari 5.0+Opera 10.60+Intern ...
- 002-创建型-01-工厂方法模式(Factory Method)
一.概述 定义一个创建对象的接口,但让实现这个接口的类来决定实例化那个类,工厂方法让类的实例化推迟到子类中进行. 工厂方法模式(FACTORY METHOD)同样属于一种常用的对象创建型设计模式,又称 ...
- 迭代器iterator-生成器generator
1. 迭代 根据记录的前面的元素的位置信息 去访问后续的元素的过程 -遍历 迭代 2. 可迭代对象 iterable 如何判断可迭代对象的3种方式 能够被迭代访问的对象 for in 常用可迭代对象- ...
- 100道iOS面试题
面试题: 1__weak什么时候用 想要在block内部变外部变量需要加__weak或者__block 2.是否使用过coreImage和coreText?如果使用过,说说你的体验(答案在另一份) 3 ...