[ 本文适用于编写 Windows 运行时应用的 Windows 8.x 和 Windows Phone 8.x 开发人员。如果你要针对 Windows 10 进行开发,请参阅 最新文档 ]
本快速入门介绍如何向使用 JavaScript 的 Windows 应用商店应用中添加标头菜单。
先决条件
定义标头和菜单
创建标头菜单的步骤:
- 将应用的标题转换为启动 Menu 控件的按钮。
- 在标题旁添加 V 型标志可表示该标题启动了菜单。
- 为菜单控件填充可将应用导航至正确部分的项。
当你完成此快速入门后,你将拥有看起来象这样的标头菜单:
![]() |
音乐应用的标头菜单 |
以下代码定义了结尾带有 V 型标志的标题以及菜单。你可以在标头菜单示例的 header-menu.html 文件中找到此代码。
<!-- Define the banner with a chevron at the end -->
<header aria-label="Header content" role="banner">
<button class="win-backbutton" aria-label="Back">
</button>
<div class="titlearea win-type-ellipsis">
<button class="titlecontainer">
<h1>
<span class="pagetitle">Music</span>
<span class="chevron win-type-x-large"></span></h1>
</button>
</header>
<!-- Define the header menu -->
<div id="headerMenu" data-win-control="WinJS.UI.Menu">
<button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'collectionMenuItem',label:'Collection'}">
</button>
<button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'marketplaceMenuItem',label:'Marketplace'}">
</button>
<button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'newsMenuItem',label:'News'}">
</button>
<hr data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'separator',type:'separator'}" />
<button data-win-control="WinJS.UI.MenuCommand" data-win-options="{id:'homeMenuItem',label:'Home'}">
</button>
</div>
将单击事件附加到标头菜单项
以下代码可将单击事件处理程序附加到菜单内容中。以下代码位于标头菜单示例的 header-menu.js 文件中。
var page = WinJS.UI.Pages.define("/html/header-menu.html", {
ready: function (element, options) {
document.querySelector(".titlearea").addEventListener("click", showHeaderMenu, false);
document.getElementById("artistsMenuItem").addEventListener("click", function() { goToSection("Collection"); }, false);
document.getElementById("albumsMenuItem").addEventListener("click", function () { goToSection("Marketplace"); }, false);
document.getElementById("songsMenuItem").addEventListener("click", function () { goToSection("News"); }, false);
document.getElementById("homeMenuItem").addEventListener("click", function () { goHome(); }, false);
}
});
// Place the flyout under the title and left-aligned
function showHeaderMenu() {
var title = document.querySelector("header .titlearea");
var menu = document.getElementById("headerMenu").winControl;
menu.anchor = title;
menu.placement = "bottom";
menu.alignment = "left";
menu.show();
}
可选:在用户导航时更新标头和子标题
为了方便查找页面,你可在用户在你的应用中导航时更新子标题。
// When navigating using the header menu for sections,
// change the subtitle to reflect the current pivot
function goToSection(section) {
WinJS.log && WinJS.log("You are viewing the " + section + " section.",
"sample", "status");
}
// Hide the subtitle if no pivot is being used
function goHome() {
WinJS.log && WinJS.log("You are home.", "sample", "status");
}
应用风格
你可以在 header-menu.css 文件中查看这些样式。
/* styles */
/* Styles for the header */
header[role=banner]
{
/* Define a grid with columns for the back button and page title. */
-ms-grid-columns: 120px 1fr;
-ms-grid-rows: 1fr;
display: -ms-grid;
}
header[role=banner] .win-backbutton
{
-ms-grid-column:1;
margin-left: 39px;
margin-top: 59px;
}
header[role=banner] .titlearea {
-ms-grid-column:2;
padding-top:43px;
}
header[role=banner] .titlecontainer
{
display:inline;
background-color:transparent;
border:none;
}
header[role=banner] .subtitlecontainer
{
display:inline;
margin-left:26px;
}
header[role=banner] .titlearea .pagetitle
{
width: calc(100% - 20px);
}
header[role=banner] .titlearea .chevron {
vertical-align:8px;
}
header[role=banner] .titlecontainer:hover
{
color: rgb(50,154,163);
}
header[role=banner] .titlecontainer:active
{
color: rgb(37,187,196);
}
#headerMenu {
width:300px;
}
摘要
在本快速入门中,你向应用添加了一个标头菜单。