js手写分页功能分享

在面试当中遇到有手写分页功能的实现

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        ul{
            list-style: none;
            display: flex;
        }
        /* ul::before{
            cont
        } */
        ul li{
            border:solid 3px #f0f0f0;
            margin: 0 10px;
            width:30px;
            line-height: 30px;
            text-align: center;
            transition: .5s;
            cursor: pointer;
        }
        ul li:hover{
            background-color: blue;
            color: white;
        }
        .active{
            background: blue;
            color: white;
        }
    </style>
</head>
<body>
    <ul>
        <li class="active">0</li>
        <li>1</li>
        <li>2</li>
        <li>3</li>
        <li>4</li>
 
    </ul>
    <script>
        var lis = document.querySelectorAll("ul li");
        for(var i = 0;i<lis.length;i++){
            // (function(){
                lis[i].index = i;
                lis[i].onclick=function(){
                    if(lis[this.index].innerHTML>1){
                        lis[Math.floor(lis.length/2)].innerHTML = lis[this.index].innerHTML;
                        for(var s = 0;s<lis.length - 1;s++){
                            lis[s].classList.remove("active");
                        }
                        lis[Math.floor(lis.length/2)].classList.add("active")
                        // if(i)
                        var o = 0;
                        for(var j = lis.length-1;j>=0;j--){
                           
                            if(j < Math.floor(lis.length/2)){
                                o++
                                lis[j].innerHTML = lis[Math.floor(lis.length/2)].innerHTML -o
                            }else if(j > Math.floor(lis.length/2)){
                                lis[j].innerHTML = lis[Math.floor(lis.length/2)].innerHTML * 1 + (j - Math.floor(lis.length/2))
                            }
                        }
                    }else{
                        for(var s = 0;s<lis.length - 1;s++){
                            lis[s].classList.remove("active");
                        }
                        lis[this.index].classList.add("active");
                       
                    }
                    // console.log(this.index)
                    // // console.log(i)
                }
            // })(i)
        }
    </script>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值