圆月山庄资源网 Design By www.vgjia.com
本文实例为大家分享了js实现分页效果的具体代码,供大家参考,具体内容如下
效果如下:
代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <style type="text/css"> </style> </head> <body> <div id="page-nation"></div> <script src="/UploadFiles/2021-04-02/page.js">page.js
var pageNation; pageNation = (function () { function extend(target, source) { for (var obj in source) { target[obj] = source[obj]; } return target; } function pageNation(options) { var defaultOptions = { el: "body",//分页父容器 page: 1, size: 10, total: 0, styUrl: 'style.css', callback: null, IsShowBtn: {//设置需要显示的按钮 choosePageSize: true,//是否显示下拉选项 IsShowPrevOrNextBtn: true,//否显示上一页下一页按钮 IsShowEnterBtn: true,//是否显示跳转输入 IsShowTotalPage:true,//是否显示总页数 } }; this.options = extend(defaultOptions, options) || {}; this.options.el = document.querySelectorAll(this.options.el)[0]; this.pageT = Math.ceil(this.options.total / this.options.size); this.init(); if (!document.querySelector("#dynamic-style")) { this.pageStyle(); } //this.options.callback(1,self.options.size,self.options.total); } var proto = pageNation.prototype || {}; proto.rander = function (page, size, total) { this.options.page = !!page "ol"); ol.classList.add("page-ol"); let p = !!page "li"); li.innerText = i; if (i == p) { li.classList.add("hover"); } ol.appendChild(li); } return ol; })(page); }; var sizeNum = function (size) {//重置size return (function () { let select = document.createElement("select"); select.setAttribute("class", "page-Select"); let s = !!size "option"); option.value = i; option.innerText = i; if (i === self.options.size) { option.setAttribute("selected", "selected"); } select.appendChild(option); } return select; })(size); }; var prevBtn = function () {//上一页按钮 return (function () { let button = document.createElement("span"); button.setAttribute("class", "prev-btn"); button.innerText = "上一页"; if (self.options.page == 1) { button.classList.add("disabled") }; return button; })(); }; var nextBtn = function () {//下一页按钮 return (function () { let button = document.createElement("span"); button.setAttribute("class", "next-btn"); button.innerText = "下一页"; if (self.options.page == self.pageT) { button.classList.add("disabled") } ; return button; })(); }; var pageMark = function () {//页码标识 return (function () { let span = document.createElement("span"); span.setAttribute("class", "pageMark"); span.innerHTML = "<strong>" + self.options.page + "</strong> / <strong>" + Math.ceil(self.options.total / self.options.size) + "</strong>"; return span; })(); }; var enterBtn = function () {//跳转按钮 return (function () { let button = document.createElement("span"); button.setAttribute("class", "enter-btn"); let ipt = document.createElement("input"); ipt.value = self.options.page; ipt.setAttribute("type", "text"); let b = document.createElement("span"); b.setAttribute("class", "enterBtn"); b.innerText = "跳转"; button.appendChild(ipt); button.appendChild(b); return button; })(); }; let page_nation = self.options.el.querySelector(".page-nation"); if (page_nation != null && page_nation != "undefind") { page_nation.innerHTML = ""; } else { page_nation = document.createElement("div"); page_nation.classList.add("page-nation"); self.options.el.appendChild(page_nation); } page_nation.appendChild(new pageNum(self.options.page)); self.options.IsShowBtn.choosePageSize&&page_nation.appendChild(new sizeNum(self.options.size)); self.options.IsShowBtn.IsShowPrevOrNextBtn&&page_nation.appendChild(new prevBtn()); self.options.IsShowBtn.IsShowPrevOrNextBtn&&page_nation.appendChild(new nextBtn()); page_nation.appendChild(new pageMark()); self.options.IsShowBtn.IsShowEnterBtn&&page_nation.appendChild(new enterBtn()); self.page_nation = document.querySelector(".page-nation"); this.switchPage(self.options.callback); this.switchSize(self.options.callback); return self; }; proto.pageStyle = function () { let headDoc = document.querySelector("head"); var linkTag = document.createElement("link"); var cssURL = this.options.styUrl; linkTag.id = 'dynamic-style'; linkTag.href = cssURL; linkTag.setAttribute('rel', 'stylesheet'); linkTag.setAttribute('media', 'all'); linkTag.setAttribute('type', 'text/css'); headDoc.appendChild(linkTag); }; proto.switchPage = function (fun) { let self = this; let lis = this.page_nation.children[0].children; for (let i = 0, len = lis.length; i < len; i++) { lis[i].onclick = function () { self.options.page = this.innerText; self.rander(self.options.page, self.options.size, self.options.total); }; } this.options.el.querySelectorAll(".prev-btn")[0].onclick = function (e) { //console.time("rr"); self.options.page = --self.options.page; self.rander(self.options.page, self.options.size, self.options.total); //console.timeEnd("rr"); }; this.options.el.querySelectorAll(".next-btn")[0].onclick = function (e) { self.options.page = ++self.options.page; self.rander(self.options.page, self.options.size, self.options.total); }; this.options.el.querySelectorAll(".enterBtn")[0].onclick = function (e) { let val = this.previousElementSibling.value; if (!val || val > self.pageT || val < 1) { alert("参数超出范围!"); return false; } else { self.options.page = val; self.rander(self.options.page, self.options.size, self.options.total); } }; fun(self.options.page, self.options.size, self.options.total); }; proto.switchSize = function (fun) { let self = this; let lis = this.page_nation.children[1]; lis.onchange = function () { self.options.size = Number(this.value); self.rander(1, self.options.size, self.options.total); fun(1, self.options.size, self.options.total); } }; return pageNation; })();以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
标签:
js,分页
圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com
暂无评论...
更新日志
2024年12月23日
2024年12月23日
- 小骆驼-《草原狼2(蓝光CD)》[原抓WAV+CUE]
- 群星《欢迎来到我身边 电影原声专辑》[320K/MP3][105.02MB]
- 群星《欢迎来到我身边 电影原声专辑》[FLAC/分轨][480.9MB]
- 雷婷《梦里蓝天HQⅡ》 2023头版限量编号低速原抓[WAV+CUE][463M]
- 群星《2024好听新歌42》AI调整音效【WAV分轨】
- 王思雨-《思念陪着鸿雁飞》WAV
- 王思雨《喜马拉雅HQ》头版限量编号[WAV+CUE]
- 李健《无时无刻》[WAV+CUE][590M]
- 陈奕迅《酝酿》[WAV分轨][502M]
- 卓依婷《化蝶》2CD[WAV+CUE][1.1G]
- 群星《吉他王(黑胶CD)》[WAV+CUE]
- 齐秦《穿乐(穿越)》[WAV+CUE]
- 发烧珍品《数位CD音响测试-动向效果(九)》【WAV+CUE】
- 邝美云《邝美云精装歌集》[DSF][1.6G]
- 吕方《爱一回伤一回》[WAV+CUE][454M]