Skills/JavaScript

221116 nav_fixed.html

개발자 윤구나 2022. 11. 16. 15:24
<!DOCTYPE html>
<html lang="ko">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>nav_fixed</title>
    <script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <script src="./js/jquery.scrollTo.min.js"></script>
    <script>
      $(function () {
        $(window).scroll(function () {
          if ($(window).scrollTop() >= 200) {
            $(".lnb").addClass("fixed");
            $(".btn_top").fadeIn(300);
          } else {
            $(".lnb").removeClass("fixed");
            $(".btn_top").fadeOut(300);
          }
        });

        $(".btn_top").click(function (e) {
          e.preventDefault();

          /*
          $('html, body').stop().animate({
            scrollTop: 0
          }, 1000)
          */

          // $(window).scrollTo({top:0, left:0}, 500)

          $(window).scrollTo(this.hash || 0, 500);
        });
      });
    </script>

    <style>
      * {
        margin: 0;
        padding: 0;
      }

      a {
        text-decoration: none;
      }

      body {
        height: 3000px;
      }

      .header {
        width: 100%;
        height: 200px;
        background-color: yellow;
        text-align: center;
        line-height: 200px;
      }

      .lnb {
        width: 1000px;
        height: 50px;
        background-color: black;
        color: white;
        text-align: center;
        line-height: 50px;
        margin: 0 auto;
      }

      .fixed {
        position: fixed;
        top: 0;
        left: 50%;
        margin-left: -500px;
      }
      /* 미리 fixed 스타일 잡아두기 */

      .btn_top {
        display: block;
        width: 30px;
        height: 30px;
        background-color: red;
        color: white;
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        line-height: 30px;
        position: fixed;
        bottom: 20px;
        right: 15px;
        /* display: none; */
      }
    </style>
  </head>
  <body>
    <header class="header" id="header">
      <h1>header</h1>
    </header>

    <nav class="lnb">
      <h1>nav_lnb</h1>
    </nav>

    <a href="#header" class="btn_top">TOP</a>
  </body>
</html>