使MDC抽屉显示在顶部应用栏下方

我按照示例代码here将可解雇的抽屉放在顶部的应用栏下方,但这不起作用。

这是我尝试过的:

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

cupid_llh 回答:使MDC抽屉显示在顶部应用栏下方

要在可解开抽屉的侧面放置标题,您需要以下结构:

// 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)中。

我看着this example's HTML来看看他们自己是怎么做的

,

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

首先,关键点:

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

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

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

main {
  margin-left: 255px;
}

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

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

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

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

本文链接:https://www.f2er.com/2803289.html

大家都在问