美文网首页
回到顶部

回到顶部

作者: 里昂的凍檸茶 | 来源:发表于2020-02-28 17:54 被阅读0次
    <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>
    

    相关文章

      网友评论

          本文标题:回到顶部

          本文链接:https://www.haomeiwen.com/subject/yczlhhtx.html