【问题标题】:Get MDC Drawer To Appear Below Top App Bar让 MDC 抽屉出现在顶部应用栏下方
【发布时间】:2020-04-26 04:28:38
【问题描述】:

我按照示例代码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>

【问题讨论】:

    标签: html css material-design mdc-components


    【解决方案1】:

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

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

    【讨论】:

    • 可以,但抽屉需要出现在下方顶部应用栏
    【解决方案2】:

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

    首先,关键位:

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

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

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

    main {
      margin-left: 255px;
    }
    

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-27
      • 2023-01-16
      • 2019-03-05
      • 2019-12-05
      • 1970-01-01
      • 1970-01-01
      • 2015-01-15
      相关资源
      最近更新 更多