您好,登錄后才能下訂單哦!
這篇文章主要介紹原生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 = `<a class="lmw-current" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${page}</a>` for(var i=1;i<=3;i++){ if(page-i>1){ str = `<a attr-action="setWhat" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${page-i}</a>`+str } if(page+i<total){ str = str+`<a attr-action="setWhat" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${(page+i)}</a>` } }; if(page-4>1){ str = '<span>...</span>'+str }; if(page+4<total){ str = str+'<span>...</span>' }; if(page>1){ str = `<a class="lmw-pre" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >上一頁</a><a attr-action="setWhat" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >1</a>`+str }; if(page<total){ str = str+`<a attr-action="setWhat" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >${total}</a><a class="lmw-add" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >下一頁</a>` }; return str } //上一頁方法 pageFunc.prototype.prevClick = function(){ var total = this.total var va = --this.currentPage var newret = this.createPage(va, total) document.getElementById('page-coat').innerHTML = newret this.myFunc(va) } //下一頁方法 pageFunc.prototype.addClick = function(){ var total = this.total var va = ++this.currentPage var newret = this.createPage(va, total) document.getElementById('page-coat').innerHTML = newret this.myFunc(va) }; //點擊方法 pageFunc.prototype.aClick = function(_this){ var total = this.total var va = parseInt(_this.innerText); this.currentPage = va var rootele = this.createPage(va, total) document.getElementById('page-coat').innerHTML = rootele this.myFunc(va) }; //二:封裝事件代理方法 function $on(dom, event, listeners) { $addEvent(dom, event, function(e) { var e = e || window.event, src = e.target || e.srcElement, action, returnVal; while (src && src !== dom) { action = src.getAttribute('attr-action') || src.getAttribute('class') ; if (listeners[action]) { returnVal = listeners[action]({ src : src, e : e, action : action }); if (returnVal === false) { break; } } src = src.parentNode; } }); }; //1、封裝跨瀏覽器事件綁定方法 function $addEvent(obj, type, handle) { if(!obj || !type || !handle) { return; } if( obj instanceof Array) { for(var i = 0, l = obj.length; i < l; i++) { $addEvent(obj[i], type, handle); } return; } if( type instanceof Array) { for(var i = 0, l = type.length; i < l; i++) { $addEvent(obj, type[i], handle); } return; } //2、解決IE中this指向window的問題 function createDelegate(handle, context) { return function() { return handle.apply(context, arguments); }; } if(window.addEventListener) { var wrapper = createDelegate(handle, obj); obj.addEventListener(type, wrapper, false); } else if(window.attachEvent) { var wrapper = createDelegate(handle, obj); obj.attachEvent("on" + type, wrapper); } else { obj["on" + type] = handle; } };
使用方法:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>分頁效果</title> <style type="text/css"> #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; } </style> </head> <body class="l-bg2"> <div id="page-coat"> </div> </body> <script type="text/javascript" src="js/jquery-1.7.2.min.js"></script> <script type="text/javascript" src="js/public.js"></script> <script type="text/javascript"> var conf = { 'total':100, 'click':function(e){ //e為當前頁碼 /* $.get('/php',{"page":e},function(data){ console.log('ok') })*/ } } var page = new pageFunc(conf); </script> </html>
用原生還是比較麻煩的,為了實現業務,還得去封裝一個模仿JQ的.on事件綁定方法。寫的比較簡陋,一些配置接口沒有暴露出來,還可以再抽象暴露。
以上是“原生javascript實現分頁效果的方法”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。