首页 > 解决方案 > 让 MDC 抽屉出现在顶部应用栏下方

问题描述

我按照此处的示例代码 将可关闭的抽屉放在顶部应用程序栏下,但它不起作用。

这是我尝试过的:

// Note: these styles do not account for any paddings/margins that you may need.
body {
  display: flex;
  height: 100vh;
}

.mdc-drawer-app-content {
  flex: auto;
  overflow: auto;
  position: relative;
}

.main-content {
  overflow: auto;
  height: 100%;
}

.app-bar {
  position: absolute;
}

// only apply this style if below top app bar
.mdc-top-app-bar {
  z-index: 7;
}
<!doctype html>
<html lang="en-US">

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <title>Default Page Title</title>
  <link rel="stylesheet" type="text/css" href="drawer.css">

  <!--[if lt IE 9]>
  <script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
  <title>Test</title>
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:400,500,700,900&display=swap">
  <link rel="stylesheet" href="https://unpkg.com/material-components-web@4.0.0/dist/material-components-web.min.css">
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" href="./drawer.css">

</head>
<header class="mdc-top-app-bar app-bar" id="app-bar">
  <div class="mdc-top-app-bar__row">
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
      <button class="mdc-top-app-bar__navigation-icon mdc-icon-button material-icons" href="#">menu</button>
      <span class="mdc-top-app-bar__title">Dismissible Drawer</span>
    </section>
  </div>
</header>
<aside class="mdc-drawer mdc-drawer--dismissible mdc-top-app-bar--fixed-adjust">
  <div class="mdc-drawer__content">
    <div class="mdc-list">
      <a class="mdc-list-item mdc-list-item--activated" href="#" aria-current="page">
        <i class="material-icons mdc-list-item__graphic" aria-hidden="true">inbox</i>
        <span class="mdc-list-item__text">Inbox</span>
      </a>
      <a class="mdc-list-item" href="#">
        <i class="material-icons mdc-list-item__graphic" aria-hidden="true">send</i>
        <span class="mdc-list-item__text">Outgoing</span>
      </a>
      <a class="mdc-list-item" href="#">
        <i class="material-icons mdc-list-item__graphic" aria-hidden="true">drafts</i>
        <span class="mdc-list-item__text">Drafts</span>
      </a>
    </div>
  </div>
</aside>

<div class="mdc-drawer-app-content mdc-top-app-bar--fixed-adjust">
  <main class="main-content" id="main-content">
    App Content
  </main>
</div>

<body>

  <script src="https://unpkg.com/material-components-web@4.0.0/dist/material-components-web.min.js"></script>

  <script>
    // Instantiate MDC Drawer
    const drawerEl = document.querySelector('.mdc-drawer');
    const drawer = new mdc.drawer.MDCDrawer.attachTo(drawerEl);

    // Instantiate MDC Top App Bar (required)
    const topAppBarEl = document.querySelector('.mdc-top-app-bar');
    const topAppBar = new mdc.topAppBar.MDCTopAppBar.attachTo(topAppBarEl);

    topAppBar.setScrollTarget(document.querySelector('.main-content'));
    topAppBar.listen('MDCTopAppBar:nav', () => {
      drawer.open = !drawer.open;
    });
  </script>
</body>

</html>

标签: htmlcssmaterial-designmdc-components

解决方案


为了在可解雇抽屉的一侧有标题,您需要以下结构:

// Instantiate MDC Drawer
const drawerEl = document.querySelector('.mdc-drawer');
const drawer = new mdc.drawer.MDCDrawer.attachTo(drawerEl);

// Instantiate MDC Top App Bar (required)
const topAppBarEl = document.querySelector('.mdc-top-app-bar');
const topAppBar = new mdc.topAppBar.MDCTopAppBar.attachTo(topAppBarEl);

topAppBar.setScrollTarget(document.querySelector('.main-content'));
topAppBar.listen('MDCTopAppBar:nav', () => {
  drawer.open = !drawer.open;
});
// Note: these styles do not account for any paddings/margins that you may need.
body {
  display: flex;
  height: 100vh;
}

.mdc-drawer-app-content {
  flex: auto;
  overflow: auto;
  position: relative;
}

.main-content {
  overflow: auto;
  height: 100%;
}

.app-bar {
  position: absolute;
}

// only apply this style if below top app bar
.mdc-top-app-bar {
  z-index: 7;
}
<!-- IMPORTS -->

<link rel="stylesheet"
      href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<link rel="stylesheet"
      href="https://fonts.googleapis.com/css?family=Roboto:400,500,700,900&display=swap">
<link rel="stylesheet"
      href="https://unpkg.com/material-components-web@4.0.0/dist/material-components-web.min.css">
<link rel="stylesheet"
      href="https://fonts.googleapis.com/icon?family=Material+Icons">

<!-- CONTENT -->

<aside class="mdc-drawer mdc-drawer--dismissible">
  <div class="mdc-drawer__content">
    <div class="mdc-list">
      <a class="mdc-list-item mdc-list-item--activated"
         href="#"
         aria-current="page">
        <i class="material-icons mdc-list-item__graphic"
           aria-hidden="true">inbox</i>
        <span class="mdc-list-item__text">Inbox</span>
      </a>
      <a class="mdc-list-item"
         href="#">
        <i class="material-icons mdc-list-item__graphic"
           aria-hidden="true">send</i>
        <span class="mdc-list-item__text">Outgoing</span>
      </a>
      <a class="mdc-list-item"
         href="#">
        <i class="material-icons mdc-list-item__graphic"
           aria-hidden="true">drafts</i>
        <span class="mdc-list-item__text">Drafts</span>
      </a>
    </div>
  </div>
</aside>

<div class="mdc-drawer-app-content">
  <header class="mdc-top-app-bar app-bar"
          id="app-bar">
    <div class="mdc-top-app-bar__row">
      <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
        <button class="mdc-top-app-bar__navigation-icon mdc-icon-button material-icons"
                href="#">menu</button>
        <span class="mdc-top-app-bar__title">Dismissible Drawer</span>
      </section>
    </div>
  </header>
  <main class="main-content"
        id="main-content">
    App Content
  </main>
</div>

<!-- IMPORTS -->
<script src="https://unpkg.com/material-components-web@4.0.0/dist/material-components-web.min.js"></script>

使这项工作的更改:

  • 首先,您不需要课程mdc-top-app-bar--fixed-adjust,因为我们不再需要调整项目
  • 接下来,将标头本身移动到app content(此处称为mdc-drawer-app-content)中。

我查看了这个示例的 HTML以了解他们自己是如何做到的


推荐阅读