JS小功能系列12楼层定位

<!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>
        * {
            margin: 0;
            padding: 0;
        }

        .head {
            width: 100%;
            height: 10px;
            padding: 10px;
            margin: 10px;
            border: 10px solid orange;
        }

        .main .wrap {
            width: 1000px;
            margin: auto;
        }

        .main .wrap>div {
            width: 100%;
            height: 300px;
        }

        .main .wrap div:nth-of-type(odd) {
            background: red;
        }

        .main .wrap div:nth-of-type(even) {
            background: orange;
        }

        .side {
            position: fixed;
            top: 100px;
            left: 10px;
        }

        .side span {
            display: block;
            padding: 10px 20px;
            font-size: 30px;
            color: #09c;
            background: greenyellow;
            border-bottom: 1px solid red;
        }

        .active {
            background: blue;
        }
    </style>
    <script>
        window.onload = function () {
            var floor = document.querySelectorAll(".floor"),
                sideSpan = document.querySelectorAll(".side span"),
                head = document.querySelector(".head"),
                body = document.body,
                count = 0;

            for (var i = 0, len = sideSpan.length; i < len; i++) {
                sideSpan[i].index = i;
                   body.scrollTop=0;
                sideSpan[i].onclick = function () {
                    var floorDistance = floor[this.index].offsetTop;

                    var roll = setInterval(function () {
                        body.scrollTop = count++;
                        if (count == floorDistance)
                            clearInterval(roll);
                    }, 1)
                    for (var j = 0, len2 = floor.length; j < len2; j++) {
                        if (this.index != j) sideSpan[j].className = "";
                    }
                    sideSpan[this.index].className = "active";
                }

            }


        }

    </script>
</head>

<body>
    <div class="head">
        头部
    </div>
    <div class="main">
        <div class="wrap">
            <div class="floor">1</div>
            <div class="floor">2</div>
            <div class="floor">3</div>
            <div class="floor">4</div>
            <div class="floor">5</div>
            <div class="floor">6</div>
        </div>
        <div class="side">
            <span>1</span>
            <span>2</span>
            <span>3</span>
            <span>4</span>
            <span>5</span>
            <span>6</span>
        </div>
    </div>
</body>

</html>

转载于:https://www.cnblogs.com/xingkongly/p/7672570.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值