我正在尝试使用MDCMenu's docs中的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>
答案 0 :(得分:0)
您通过将setAbsolutePosition
包装在mdc-menu
中来覆盖mdc-menu-surface--anchor
。以下示例删除了anchor元素,因此您可以使用setAbsolutePosition
。
const menu = mdc.menu.MDCMenu.attachTo(document.querySelector('.mdc-menu'));
menu.setAbsolutePosition(0, 100);
menu.open = true;
const btn = document.querySelector('#menu-button');
btn.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">
<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">
<button id="menu-button" class="material-icons mdc-top-app-bar__action-item" aria-label="More" alt="More">more_vert</button>
</section>
</div>
</header>
<div class="mdc-menu mdc-menu-surface" tabindex="-1">
<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>
</body>