使用CDN的Web模式抽屉示例的材料组件

Abe*_*ejo 3 html javascript material-design material-components material-components-web

将Web材料组件用于Web CDN以进行前端设计的示例没有用处.

我发现了这个模态抽屉的演示,但它似乎没有使用已发布的CSS和JavaScript CDN. 演示:模态抽屉演示

如何使用Material Components的CDN实现模态抽屉?

ben*_*nvc 12

使用unpkg bundle而不是通过webpack使用单个组件来使用MDC有点困难." 入门"文档有所帮助,但翻译文档以供各种组件使用仍然很困难.最棘手的部分是弄清楚如何实例化各种组件(因为没有一种适合所有方法).这是一个使用unpkg来模拟抽屉组件的快速示例.

const drawer = mdc.drawer.MDCDrawer.attachTo(document.querySelector('.mdc-drawer'));
const topAppBar = mdc.topAppBar.MDCTopAppBar.attachTo(document.querySelector('#app-bar'));
topAppBar.listen('MDCTopAppBar:nav', () => {
  drawer.open = !drawer.open;
});
Run Code Online (Sandbox Code Playgroud)
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Material Modal Drawer Example</title>
  <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
  <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700">
  <link href="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.css" rel="stylesheet">
  <script src="https://unpkg.com/material-components-web@latest/dist/material-components-web.min.js"></script>
</head>

<body>
  
  <aside class="mdc-drawer mdc-drawer--modal">
    <div class="mdc-drawer__content">
      <nav 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>
      </nav>
    </div>
  </aside>

  <div class="mdc-drawer-scrim"></div>
  
  <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">
          <a href="#" class="demo-menu material-icons mdc-top-app-bar__navigation-icon">menu</a>
          <span class="mdc-top-app-bar__title">Title</span>
        </section>
      </div>
    </header>
    
    <main class="main-content" id="main-content">
      <div class="mdc-top-app-bar--fixed-adjust">
        App Content
      </div>
    </main>
    
  </div>

</body>

</html>
Run Code Online (Sandbox Code Playgroud)

  • `mdc`对象实际上几乎是不可发现的。大部分示例代码都使用[JavaScript导入](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import),例如从@@ material /中导入{MDCDrawer}抽屉”;`。假设总是使用库的本地副本。仅当您弄清楚了mdc对象的子组件时,语句const cabinet = mdc.drawer.MDCDrawer.attachTo(document.querySelector('。mdc-drawer'));才有意义。我敢打赌,许多人很难找到它。 (2认同)