1

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

这是我尝试过的:

// 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>

4

2 回答 2

0

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

// 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以了解他们自己是如何做到的

于 2020-01-08T16:02:31.407 回答
0

这是对我有用的自定义 CSS。我的是一个永久抽屉,所以如果你想让抽屉被关闭,你可能需要一些 JS 来动态应用更改,但它解决了布局问题。

首先,关键位:

.mdc-drawer {
  position: fixed;
  top: 64px;
}

由于抽屉现在是固定位置的,它不再影响布局,并且顶部栏看到它有空间填充页面的整个宽度。将抽屉向下推 64 像素可防止其覆盖顶部栏。

然而,这有一些必须处理的副作用。首先,最明显的是,您的主要内容与顶栏所做的相同——它认为它有空间来填满整个页面,所以它在抽屉后面丢失了。只需将其推过去即可解决此问题:

main {
  margin-left: 255px;
}

其次,您会注意到,如果您调整视口的大小,抽屉在应该显示滚动条时不会显示,而当滚动条出现时,它不会一直到底部。这是因为您已将抽屉底部从页面上推下。

要解决此问题,请缩短抽屉的可滚动内容:

.mdc-drawer__content {
  height: calc(100% - 128px);
}

(我本来希望它是 100% - 64px,但由于某种原因它需要 128。我还没弄清楚为什么。)

于 2021-03-01T02:14:07.947 回答