JavaScript实现楼层效果

       * {
            margin: 0;
            padding: 0;
        }
        
        html,
        body {
            width: 100%;
            height: 100%;
        }
        
        ul {
            width: 100%;
            height: 100%;
        }
        
        ul>li {
            list-style: none;
            width: 100%;
            height: 100%;
            font-size: 100px;
            text-align: center;
        }
        
        ol {
            position: fixed;
            left: 10px;
            top: 50%;
            transform: translateY(-50%);
        }
        
        ol>li {
            list-style: none;
            width: 100px;
            line-height: 40px;
            text-align: center;
            border: 1px solid #000;
        }
        
        .selected {
            background: skyblue;
        }
    <ul>
        <li>我是第1层</li>
        <li>我是第2层</li>
        <li>我是第3层</li>
        <li>我是第4层</li>
        <li>我是第5层</li>
    </ul>

    <ol>
        <li class="selected">第1层</li>
        <li>第2层</li>
        <li>第3层</li>
        <li>第4层</li>
        <li>第5层</li>
    </ol>
        // 1.初始化楼层的颜色
        let oPages = document.querySelectorAll("ul>li");
        let colorArr = ['green', 'blue', 'purple', 'red', 'yellow'];
        for (let i = 0; i < oPages.length; i++) {
            let page = oPages[i];
            page.style.background = colorArr[i];
        }

        // 2.实现点击谁就选中谁
        let oItems = document.querySelectorAll("ol>li");
        let currentItem = oItems[0];

        // 获取可视区域的高度
        let screenHeight = getScreen().height;

        let timerId = null;
        for (let i = 0; i < oItems.length; i++) {
            let item = oItems[i];
            item.onclick = function() {
                currentItem.className = "";
                this.className = "selected";
                currentItem = this;
                // 实现滚动
                // window.scrollTo(0, i * screenHeight);
                // 注意点: 通过documentElement.scrollTop来实现网页滚动, 在设置值的时候不能添加单位
                // document.documentElement.scrollTop = i * screenHeight + "px";
                // document.documentElement.scrollTop = i * screenHeight;
                clearInterval(timerId);
                timerId = setInterval(function() {
                    let begin = document.documentElement.scrollTop;
                    let target = i * screenHeight;
                    let step = (target - begin) * 0.2;
                    begin += step;
                    if (Math.abs(Math.floor(step)) <= 1) {
                        clearInterval(timerId);
                        document.documentElement.scrollTop = i * screenHeight;
                        return;
                    }
                    document.documentElement.scrollTop = begin;
                }, 50);
            }
        }

        //获取浏览器视口宽高
        function getScreen() {
            let width, height;
            if (window.innerWidth) {
                width = window.innerWidth;
                height = window.innerHeight;
            } else if (document.compatMode === "BackCompat") {
                width = document.body.clientWidth;
                height = document.body.clientHeight;
            } else {
                width = document.documentElement.clientWidth;
                height = document.documentElement.clientHeight;
            }
            return {
                width: width,
                height: height
            }
        }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小白小白从不日白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值