3300.me

scroll_behavior

last update: 2022/07/29

sample


<!DOCTYPE html>
<html>
  <head>
    <title>scroll-behavior</title>
    <style>
      html,
      body {
        margin: 0;
        padding: 0;
        height: 100%;
      }
      html {
        scroll-behavior: smooth;
      }
      .header {
        width: 100%;
        height: 60px;
        position: fixed;
      }
      .header ul {
        display: flex;
        align-items: center;
        gap: 20px;
        padding: 0;
      }
      .header li {
        list-style: none;
      }
      .header a {
        color: #fff;
      }
      .inner {
        position: relative;
      }
      .overlay {
        width: 100%;
        height: 60px;
        position: absolute;
        opacity: 0.6;
        background: #000;
      }
      .menu {
        width: 100%;
        display: grid;
        place-content: center;
        position: absolute;
      }
      .content {
        padding-top: 60px;
      }
      section {
        height: calc(100vh - 60px);
        border: #000 1px solid;
      }
      section a {
        scroll-margin-top: 60px;
      }
      h1 {
        margin: 0;
        padding: 30px;
      }
    </style>
  </head>
  <body>
    <div class="header">
      <div class="inner">
        <div class="overlay"></div>
        <div class="menu">
          <ul>
            <li><a href="#a">scroll a</a></li>
            <li><a href="#b">scroll b</a></li>
            <li><a href="#c">scroll c</a></li>
          </ul>
        </div>
      </div>
    </div>
    <div class="content">
      <section>
        <a name="a"></a>
        <h1>a</h1>
      </section>
      <section>
        <a name="b"></a>
        <h1>b</h1>
      </section>
      <section>
        <a name="c"></a>
        <h1>c</h1>
      </section>
    </div>
  </body>
</html>