点击链接scrollTop滚动条移动

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
             *{     margin: 0;    }
            ul{    width: 100%;height: 30px;position: fixed;display: none;}
            ul li{float: left;list-style: none;width: 250px;height: 30px;
                text-align: center;font-family: "微软雅黑";font-size: 20px;
                color: white;}
            div{width: 100%;
                height: 500px;
                text-align: center;
                font-size: 50px;
                line-height: 500px;
            }
            #btn{
                position: fixed;
                right: 0;
                bottom: 0;
                height: 50px;
                font-family: "微软雅黑";
                font-size: 18px;
                display: none;
            }
        </style>
    </head>
    <body>
        <ul id="ul1">
            <li style="background-color: red;" class="li1">1</li>
            <li style="background-color: black;" class="li1">2</li>
            <li style="background-color: blue;" class="li1">3</li>
            <li style="background-color: pink;" class="li1">4</li>
            <li style="background-color: green;" class="li1">5</li>
        </ul>
        <div class="diva" style="background:red;">红色区域</div>
        <div class="diva" style="background:black;">黑色区域</div>
        <div class="diva" style="background:blue;">蓝色区域</div>
        <div class="diva" style="background:pink;">粉色区域</div>
        <div class="diva" style="background:green;">绿色区域</div>
        <button id="btn">返回顶部</button>
        <script>
        /*
         原理:
           每次点击li时让li的高度值等于每个对应的div的scrollTop值
         * */
        window.onscroll = function (){
            var oUl = document.getElementById('ul1');
            var lis = document.getElementsByClassName('li1');
            var divs = document.getElementsByClassName('diva');
            var oBtn = document.getElementById('btn');
            var oScroll = document.documentElement.scrollTop||document.body.scrollTop;
            if(oScroll>200){
                oUl.style.display = 'block';
                oBtn.style.display = 'block';
            }else{
                oUl.style.display = 'none';
                oBtn.style.display = 'none';
            }
            oBtn.onclick = function (){
                document.documentElement.scrollTop = document.body.scrollTop = 0;
            }
            for (var i=0;i<lis.length;i++) {
                lis[i].index = i;//获取每一个li的索引下标
                lis[i].onclick = function (){
                    document.documentElement.scrollTop = document.body.scrollTop = (divs[this.index].offsetHeight)*this.index;
                }
            }
        }
            
        </script>
    </body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值