JS实现侧边栏点击回到指定位置的功能

  1. DEMO
    在这里插入图片描述
  2. 代码写的不太好,大佬可以帮忙修改修改哈
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    html {
        scroll-behavior: smooth
    }

    * {
        margin: 0;
        padding: 0;
        box-sizing: border-box;
    }

    .item {
        position: relative;
        width: 100%;
        font-size: 30px;
        text-align: center;
        height: 100vh;
        line-height: 100px;
    }



    .tab-item {
        cursor: pointer;
        line-height: 100px;

    }


    .red {
        background-color: red;
    }

    .blue {
        background-color: blue;
    }

    .black {
        background-color: black;
        color: white;
    }

    .yellow {
        background-color: yellow;
    }

    .tab {
        position: fixed;
        width: 100px;
        height: 400px;
        background-color: white;
        top: 30%;
        right: 10px;
        display: flex;
        flex-direction: column;
        justify-content: space-around;
        text-align: center;
    }

    .show {
        background-color: red;
    }

    .backTop {
        cursor: pointer;
        position: fixed;
        right: 30px;
        bottom: 30px;
        display: none;
        background-color: #ccc;
        width: 100px;
        line-height: 40px;
        color: white;
        animation: show .1s ease;
        text-align: center;
        z-index: 10;
    }

    @keyframes show {
        from {
            transform: scale(0);
            opacity: 0;
        }

        to {
            transform: scale(1);
            opacity: 1;
        }
    }
</style>

<body>
    <div class="backTop">回到顶部</div>
    <div id="list">
        <div class="item red">
            楼区一
        </div>
        <div class="item blue">
            楼区二
        </div>
        <div class="item black">
            楼区三
        </div>
        <div class="item yellow">
            楼区四
        </div>
    </div>
    <div class="tab">
        <div class="tab-item show">一层</div>
        <div class="tab-item">二层</div>
        <div class="tab-item">三层</div>
        <div class="tab-item">四层</div>
    </div>
    <script>
        var show = ['red', 'blue', 'black', 'yellow']
        var tab_item = document.querySelectorAll('.tab-item')
        var item = document.querySelectorAll('.item')
        var flag = true // 禁止连续点击
        var backTop = document.querySelectorAll('.backTop')[0]
        tab_item.forEach((item, index) => {
            item.addEventListener('click', scrollColor, false) //点击改变界面颜色方法
            item.index = index
        });
        function scrollColor() {
            if (flag) {
                flag = false
                console.log(flag);
                var way = item[this.index].offsetTop// 根据自定义属性index 获取侧边栏对应的大色块距离浏览器上面的距离
                var timer = setInterval(() => {
                    scrollTo(0, way) // html css设置 scroll-behavior: smooth 或者封装动画类 参考文章 https://blog.csdn.net/weixin_43625763/article/details/103478291
                    for (var i = 0; i < item.length; i++) {
                        if (way == innerHeight * i) { //  bug 当这两个值相等 清除定时器 此处需优化 似乎还存在点击 但效果还好
                            clearInterval(timer)
                            flag = true
                        }
                    }
                }, 100);
            }
        }
        document.onscroll = function () {
            for (var i = 0; i < tab_item.length; i++) {
                if (scrollY > innerHeight * i - 20) {  // -20设置的稍微提前点 因为最后一次无法触底
                    for (var j = 0; j < tab_item.length; j++) {   // 排他思想
                        tab_item[j].style.backgroundColor = 'white'
                        tab_item[j].style.color = 'black'
                    }
                    tab_item[i].style.backgroundColor = show[i]
                    if (i === 2) { // 单独处理黑色块
                        tab_item[i].style.color = 'white'
                    }
                }
            }
            if (scrollY > innerHeight) { // 按钮在滑动超过第二个色块出现
                backTop.style.display = 'block'
            } else {
                backTop.style.display = 'none'
            }
        }
        backTop.onclick = function () {
            if (flag) { // 禁止多重点击
                flag = false
                var timetop = setInterval(() => { // 设置移动到顶部
                    scrollTo(0, 0)
                    if (scrollY == 0) {
                        clearInterval(timetop)
                        flag = true
                    }
                }, 300);
            }
        }
    </script>
</body>
<html>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值