クイック スタート: ヘッダー メニューの追加 (HTML)

[ この記事は、Windows ランタイム アプリを作成する Windows 8.x および Windows Phone 8.x 開発者を対象としています。Windows 10 向けの開発を行っている場合は、「最新のドキュメント」をご覧ください]

このクイック スタートでは、JavaScript を使って、Windows ストア アプリにヘッダー メニューを追加する方法を説明します。

必要条件

ヘッダー メニューを定義する

ヘッダー メニューを作るには、次の手順を実行します。

  1. アプリのタイトルを、Menu コントロールを起動するボタンに変換します。
  2. タイトルがメニューを起動することを示すために、タイトルの横に山かっこを追加します。
  3. メニュー コントロールに、アプリを適切なセクションに移動するための項目を設定します。

このクイック スタートを完了すると、次のようなヘッダー メニューが表示されます。

音楽アプリのヘッダー メニュー
音楽アプリのヘッダー メニュー。

 

次のコードは、最後に山かっこがあるバナーとメニューを定義しています。このコードは、ヘッダー メニューのサンプルの 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">&#xe099;</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;
}

要約

このクイック スタートでは、アプリにヘッダー メニューを追加しました。

関連トピック

Menu

MenuCommand

ヘッダー メニューのサンプルに関するページ

ナビゲーション デザイン

ナビゲーションのサポート

適切な UI サーフェスの選択

Windows 8 のシルエットについて