js标题定位,自动下滑,自动上滑,值得一看

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        * {
            border: 0;
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        p {
            width: 80%;
            background-color: rgba(54, 40, 45, 0.39);
            margin: 10px auto;
            text-align: center;
            font-size: 30px;
        }

        #h3 {
            width: 100%;
            background-color: rgba(152, 125, 68, 0.53);
            text-align: center;
            margin: 10px auto;
            font-size: 50px;
            z-index: 10000;
        }

        #top {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffc382;
            margin: 0 auto;
            line-height: 50px;
            text-align: center;
            position: fixed;
            bottom: 1%;
            right: 1%;
        }

        #bottom {
            width: 50px;
            height: 50px;
            border-radius: 50%;
            background-color: #ffc382;
            margin: 0 auto;
            line-height: 50px;
            text-align: center;
            position: fixed;
            top: 1%;
            right: 1%;
        }
    </style>
</head>
<body>

<p>hello001</p>
<p>hello002</p>
<p>hello003</p>
<p>hello004</p>
<p>hello005</p>
<p>hello006</p>
<p>hello007</p>
<p>hello008</p>
<div id="h3">网站导航</div>
<p>hello009</p>
<p>hello010</p>
<p>hello011</p>
<p>hello012</p>
<p>hello013</p>
<p>hello014</p>
<p>hello015</p>
<p>hello016</p>
<p>hello017</p>
<p>hello018</p>
<p>hello019</p>
<p>hello020</p>
<p>hello021</p>
<p>hello022</p>
<p>hello023</p>
<p>hello024</p>
<p>hello025</p>
<p>hello026</p>
<p>hello027</p>
<p>hello028</p>
<p>hello029</p>
<p>hello030</p>
<p>hello031</p>
<p>hello032</p>
<p>hello033</p>
<p>hello034</p>
<p>hello035</p>
<p>hello036</p>
<p>hello037</p>
<p>hello038</p>
<p>hello039</p>
<p>hello040</p>
<p>hello041</p>
<p>hello042</p>
<p>hello043</p>
<p>hello044</p>
<p>hello045</p>
<p>hello046</p>
<p>hello047</p>
<p>hello048</p>
<p>hello049</p>
<p>hello050</p>
<p>hello051</p>
<p>hello052</p>
<p>hello053</p>
<p>hello054</p>
<p>hello055</p>
<p>hello056</p>
<p>hello057</p>
<p>hello058</p>
<p>hello059</p>
<p>hello060</p>

<div id="top">top</div>
<div id="bottom">bot</div>
<script>
    setInterval(function () {
        document.title = Math.round(window.pageYOffset);
    }, 20);
    var topname = document.getElementById("top");
    topname.onclick = function () {

        let toplen = 20;
        let topset = setInterval(function () {
            window.scrollTo(0, window.pageYOffset - toplen);
            if (window.pageYOffset <= 0) {
                clearInterval(topset);
            }
        }, 20);
    }
    var bottomname = document.getElementById("bottom");
    bottomname.onclick = function () {
        let bottomlen = 5;
        let bottomset = setInterval(function () {
            window.scrollTo(0, window.pageYOffset + bottomlen)
            if (window.pageYOffset >= 2239) {
                clearInterval(bottomset);
            }
        })
    }
    var h3name = document.getElementById("h3");
    setInterval(function () {
        if (window.pageYOffset >= (2239 - 1000)) {
            topname.style.visibility = "visible";
        } else {
            topname.style.visibility = "hidden";
        }
        if (window.pageYOffset <= 1000) {
            bottomname.style.visibility = "visible";
        } else {
            bottomname.style.visibility = "hidden";
        }

        if (window.pageYOffset >= 370) {
            h3name.style.position = "fixed";
            h3name.style.top = "2px";

        } else {
            h3name.style.position = "static"
        }


    }, 20);


</script>

</body>
</html>

结果如下:

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值