回到顶部

<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <style type="text/css">
            html, body {
              scroll-behavior: smooth;
            }
            .web{
                position: relative;
            }
            .yc{
                display: none;
            }
            .toTop{
                position: fixed;
                bottom: 40px;
                cursor: pointer;
                right: 20px;
                border: 1px solid #FF0000;
            }
            .blue{
                height: 1200px;
                width: 100%;
                background: #0000FF;
            }
            .orange{
                height: 1200px;
                width: 100%;
                background: orange;
            }
            .yellow{
                height: 1200px;
                width: 100%;
                background: yellow;
            }
            .red{
                height: 1200px;
                width: 100%;
                background: red;
            }
            .pink{
                height: 1200px;
                width: 100%;
                background: pink;
            }
            .green{
                height: 1200px;
                width: 100%;
                background: green;
            }
            
        </style>
    </head>
    <body>
        <div class="web">
            <p name="top">我是顶部呀</p>
            <div class="blue"></div>
            <div class="orange"></div>
            <div class="red"></div>
            <div class="yellow"></div>
            <div class="pink"></div>
            <div class="green"></div>
            <!-- <a href="#top">返回顶部</a> -->
            <p class="toTop yc">返回顶部</p>
        </div>
        <script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
        <script type="text/javascript">
            $(window).on("scroll",function(){
                var st = $(document).scrollTop();
                if(st>300){
                    $(".toTop").fadeIn(function(){
                        $(this).removeClass("yc");
                    })
                }else{
                    $(".toTop").fadeOut(function(){
                        $(this).addClass("yc");
                    })
                }
            })
            $(".toTop").on("click",function(){
                $("html,body").animate({"scrollTop":0},500)
            })
        </script>
    </body>
</html>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容