zoukankan      html  css  js  c++  java
  • JavaScript分页栏链接转变算法

    window.onload = function(){
        var para = {pagecount: 9, curpage: 5};
        var pageBar = document.getElementById("pageBar");
        pageBar.innerHTML = createPageBar(para);
    };
    
    function getPage(id){
        id = parseInt(id);
        var pageBar = document.getElementById("pageBar");
        pageBar.innerHTML = createPageBar({pagecount: 9, curpage: id});
    }
    
    function createPageBar(para){
        var i;
        var pageBarStr = '<div>';
        if(para.pagecount < 8){
            for(i = 1; i <= para.pagecount; i++){
                pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                + i
                +'</a>';
            }
        }else if(para.pagecount == 8){
            if(para.curpage < 5){
                for(i = 1; i < 7; i++){
                    pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                        + i
                        +'</a>';
                }
                pageBarStr += '...<a href="javascript:;" onclick="getPage(this.innerHTML)">8</a>';
            }else{
                pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">1</a>...';
                for(i = 3; i <= 8; i++){
                    pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                        + i
                        +'</a>';
                }
            }
        }else{ //pagecount > 8
            if(para.curpage < 5){
                for(i = 1; i < 7; i++){
                    pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                        + i
                        +'</a>';
                }
                pageBarStr += '...<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                    + para.pagecount
                    +'</a>';
            }else if(para.curpage > para.pagecount - 4){
                pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">1</a>...';
                for(i = para.pagecount - 5; i <= para.pagecount; i++){
                    pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                    + i
                    +'</a>';
                }
            }else{
                pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">1</a>...';
                for(i = para.curpage - 2; i < para.curpage + 3; i++){
                    pageBarStr += '<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                    + i
                    +'</a>';
                }
                pageBarStr += '...<a href="javascript:;" onclick="getPage(this.innerHTML)">'
                + para.pagecount
                +'</a>';
            }
        }
        
        pageBarStr += '</div>';
        return pageBarStr;
    };
    

      

  • 相关阅读:
    解决import tensorflow时发生DLL错误
    解决Docker Container in WSL2 发生DNS错误无法访问网络
    使用过vmware 再开启wsl2闪退处理
    70. Climbing Stairs. Leetcode
    miredo on mac
    解决Runtime Error on LeetCode
    HttpClient Get与Post请求数据
    Ubuntu16.04.1 安装MyCat
    CenterOS中安装Redis及开机启动设置
    ASP.ENT Core Linux 下 为 donet创建守护进程(转载)
  • 原文地址:https://www.cnblogs.com/realwall/p/2178861.html
Copyright © 2011-2022 走看看