2

我正在尝试使用MDCMenu 的文档menu.setAbsolutePosition(0, 100);中找到的方法,将“mdc-menu”相对(波纹管)定位到我的顶栏上的溢出按钮,但它似乎没有做太多(实际上使元素卡在屏幕外),无论我设置什么值。

我创建了一个片段来说明这个问题:

<link rel="stylesheet" type="text/css" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">
<script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">

<header class="mdc-top-app-bar">
  <div class="mdc-top-app-bar__row">
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-start">
      <a href="#" class="material-icons mdc-top-app-bar__navigation-icon">menu</a>
      <span class="mdc-top-app-bar__title">Title</span>
    </section>
    <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
      <div id="demo-menu" class="mdc-menu-surface--anchor">
        <a href="#" class="material-icons mdc-top-app-bar__action-item" aria-label="More" alt="More">more_vert</a>
        <div class="mdc-menu mdc-menu-surface">
          <ul class="mdc-list" role="menu" aria-hidden="true" aria-orientation="vertical">
            <li class="mdc-list-item" role="menuitem">
              <span class="mdc-list-item__text">A Menu Item</span>
            </li>
          </ul>
        </div>
      </div>
    </section>
  </div>
</header>

<script>
  var MDCMenu = mdc.menu.MDCMenu.attachTo(document.querySelector('.mdc-menu'));
  MDCMenu.open = true;
  // MDCMenu.hoistMenuToBody(); 
  MDCMenu.setAbsolutePosition(0, 100);
</script>

4

1 回答 1

0

您通过将MDC 菜单setAbsolutePosition包装在. 以下示例删除了锚元素,以便您可以使用.mdc-menu-surface--anchorsetAbsolutePosition

const bar = mdc.topAppBar.MDCTopAppBar.attachTo(document.querySelector('.mdc-top-app-bar'));
const menu = mdc.menu.MDCMenu.attachTo(document.querySelector('.mdc-menu'));
menu.setAbsolutePosition(0, 100);
menu.open = true;

document.querySelector('#menu-button').addEventListener('click', () => {
  menu.open = !menu.open;
});
<head>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" type="text/css" href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css">
  <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>

<body>

  <header class="mdc-top-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="material-icons mdc-top-app-bar__navigation-icon mdc-icon-button" aria-label="Open navigation menu">menu</button>
        <span class="mdc-top-app-bar__title">Title</span>
      </section>
      <section class="mdc-top-app-bar__section mdc-top-app-bar__section--align-end" role="toolbar">
        <button id="menu-button" class="material-icons mdc-top-app-bar__action-item mdc-icon-button" aria-label="Open menu">more_vert</button>
      </section>
    </div>
  </header>

  <div class="mdc-menu mdc-menu-surface">
    <ul class="mdc-list" role="menu" aria-hidden="true" aria-orientation="vertical" tabindex="-1">
      <li class="mdc-list-item" role="menuitem">
        <span class="mdc-list-item__text">A Menu Item</span>
      </li>
    </ul>
  </div>

</body>

于 2018-11-29T19:36:59.637 回答