示例工程 56-responsive-nav

56-responsive-nav/demo-01-nav/index.html

html · 1.5 KB

运行:桌面与移动导航

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

<!doctype html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>桌面与移动导航</title>
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <header class="header">
      <a class="brand" href="#top">ASTER</a>
      <nav class="nav" aria-label="主导航">
        <a href="#top">首页</a>
        <a href="#work">作品</a>
        <a href="#contact">联系</a>
      </nav>
      <button class="toggle" type="button" aria-haspopup="dialog" aria-controls="drawer">菜单</button>
    </header>

    <dialog class="drawer" id="drawer" aria-label="移动导航">
      <div class="drawer-head">
        <strong>菜单</strong>
        <button class="close" type="button" aria-label="关闭">×</button>
      </div>
      <nav>
        <a href="#top">首页</a>
        <a href="#work">作品</a>
        <a href="#contact">联系</a>
      </nav>
    </dialog>

    <main class="content">
      <h1>缩放窗口</h1>
      <p>桌面显示横排导航,窄屏显示菜单按钮。</p>
    </main>

    <script>
      const toggle = document.querySelector(".toggle");
      const drawer = document.querySelector("#drawer");
      const close = drawer.querySelector(".close");
      toggle.addEventListener("click", () => drawer.showModal());
      close.addEventListener("click", () => drawer.close());
      drawer.addEventListener("close", () => toggle.focus());
    </script>
  </body>
</html>