zoukankan      html  css  js  c++  java
  • 网页吸顶&&回到顶部

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
        <style>
            *{margin:0;padding:0;}
            #header{
                 100%;
                height: 200px;
            }

            ul,li{list-style: none;}
            #nav{
                 100%;
                height: 80px;
                background: #000;
            }
            #nav>ul>li{
                float: left;
                text-align: center;
                line-height: 80px;
                color: #fff;
                 80px;
            }

    #t{
                 70px;
                height: 60px;
                background: red;
                position: fixed;
                right: 0;
                text-align: center;
                line-height: 60px;
                color: #fff;
                top: 400px;
                display: none;
            }
        </style>
    </head>
    <body style="height:3000px;">
        <div id="header"></div>
        <div id="nav">
            <ul>
                <li>首页</li>
                <li>文档</li>
                <li>我的</li>
                <li>设置</li>
            </ul>
        </div>

     <div id="t">回到顶部</div>
    </body>
    </html>
    <script>
     var oNav = document.getElementById("nav");

    var box = document.getElementById("t");
        window.onscroll = function () {
            var sTop = document.documentElement.scrollTop || document.body.scrollTop;
            if(sTop>=200){
                oNav.style.position = "fixed";
                oNav.style.top = 0;

    box.style.display = "block";
            }else{
                oNav.style.position = "static";

     box.style.display = "none";
            }
        }
        box.onclick = function(){
            document.documentElement.scrollTop  =  document.body.scrollTop  = 0;
        }
    </script>

  • 相关阅读:
    我工作三年了,该懂并发了!
    代理,一文入魂
    非典型算法题,用程序和电脑玩一个游戏
    详解command设计模式,解耦操作和回滚
    matplotlib画图教程,设置坐标轴标签和间距
    详解工程师不可不会的LRU缓存淘汰算法
    详解深度学习感知机原理
    详解gitignore的使用方法,让你尽情使用git add .
    算法题 | 你追我,如果你追到我……那就算你赢了
    险些翻车,差一点没做出来的基础算法题
  • 原文地址:https://www.cnblogs.com/ngdty/p/9477461.html
Copyright © 2011-2022 走看看