Minimal Green

はてなブログテーマ『Minimal Green』のデモブログ。はてなブログカスタマイズや、HTML、CSSなどについて書いています。

スマホに使える!2階層対応レスポンシブメニュー

f:id:minimalgreen:20190815235709j:plain
スマホでのメニュー表示はいつも悩ましいものです。スマホの画面サイズが大きくなったとはいえ、やはり表示領域は狭く限られています。

特にカテゴリーの数が多くメニューが多階層にわたるサイトやブログのメニュー表示をどうするか。

今回はたくさんのメニュー項目をコンパクトにまとめて表示できるメニューのカスタマイズです。

blog.minimal-green.com

ベースは↑のメニューを使い、若干デザインと機能を追加しました。


変更点

変更点
  • ハンバーガーメニューのデザイン変更
  • スマホ表示で2階層目の項目をタップで開閉するように

メニューの動き

動きは以下のgifをご覧下さい。
f:id:minimalgreen:20190815235114g:plain

HTML&jQuery

以下のコードをはてなブログ管理画面>デザイン>カスタマイズ>ヘッダ>タイトル下に入れて下さい。

<div class="drawer_menu">
	<!-- コピーライト消さないように!
		Zarigani Design Office Drawer Menu
		Copyright 2018 Zarigani Design Office
                Customized by minimalgreen

		Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

		The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

		THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
		コピーライトここまで -->
	<div class="drawer_bg"></div>
	<button type="button" class="drawer_button">
		<span class="drawer_bar drawer_bar1"></span>
		<span class="drawer_bar drawer_bar2"></span>
		<span class="drawer_bar drawer_bar3"></span>
		<span class="drawer_menu_text drawer_text">MENU</span>
		<span class="drawer_close drawer_text">CLOSE</span>
	</button>
	<nav class="drawer_nav_wrapper">
		<ul class="drawer_nav">
			<li>
				<a href="#">メニュー1 </a>
			</li>
			<li>
				<a href="#">メニュー2 </a>
				<span class="touch-btn"><i class="blogicon-chevron-down lg"></i></span>
				<ul class="second-level">
					<li>
						<a href="">サブメニュー1</a>
					</li>
					<li>
						<a href="">サブメニュー2</a>
					</li>
					<li>
						<a href="">サブメニュー3</a>
					</li>
				</ul>
			</li>
			<li>
				<a href="#">メニュー3 </a>
				<span class="touch-btn"><i class="blogicon-chevron-down lg"></i></span>
				<ul class="second-level">
					<li>
						<a href="">サブメニュー1</a>
					</li>
					<li>
						<a href="">サブメニュー2</a>
					</li>
					<li>
						<a href="">サブメニュー3</a>
					</li>
				</ul>
			</li>
			<li>
				<a href="#">メニュー4 </a>
			</li>
			<li>
				<a href="#">メニュー5 </a>
			</li>
			<li>
				<a href="#">メニュー6 </a>
			</li>
		</ul>
		<!-- PC非表示フリースペース -->
		<div class="pc-hidden">
			<div class="drawer-title">Profile</div>
			<p>
				<img src="プロフィール画像URL" alt="プロフィールアイコン " class="profile-icon">
			</p>
			<p>id:プロフィール名<br>プロフィール説明文を入れて下さい。
			</p>
			<div class="center">
				<div class="follow-buttons">
					<div class="center">\ Follow me /</div>
					<a class="facebook" href="facebookページURL" target="">
						<i class="blogicon-facebook lg"></i>
						<span class="inner-text">Facebook</span>
					</a>
					<a class="instagram" href="https://www.instagram.com/ユーザー名" target="">
						<i class="blogicon-instagram lg"></i>
						<span class="inner-text">Instagram</span>
					</a>
					<a class="twitter" href="https://twitter.com/ユーザー名" target="">
						<i class="blogicon-twitter lg"></i>
						<span class="inner-text">Twitter</span>
					</a>
					<a class="hatena" href="http://blog.hatena.ne.jp/ユーザー名/ブログドメイン/subscribe" target="">
						<i class="blogicon-hatenablog lg"></i>
						<span class="inner-text">Hatena</span>
					</a>
					<a class="feedly" href="https://feedly.com/i/subscription/feed/http:ブログドメイン/feed" target="">
						<i class="blogicon-rss lg"></i>
						<span class="inner-text">Feedly</span>
					</a>
				</div>
			</div>
		</div><!-- PC非表示フリースペースここまで -->
	</nav>
</div>
<!-- jQuery読み込み -->
<script src="https://code.jquery.com/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
$(function () {

	$('.drawer_button').click(function () {
		$(this).toggleClass('active');
		$('.drawer_bg').fadeToggle();
		$('nav').toggleClass('open');
	})
	$('.drawer_bg').click(function () {
		$(this).fadeOut();
		$('.drawer_button').removeClass('active');
		$('nav').removeClass('open');
	});
	$(".touch-btn").on("click", function () {
		$(this).next().slideToggle();
	});
})
</script>

メニューやプロフィールのリンク先は適宜各自で変更して下さい。

CSS

デザインCSSに以下のコードを追記します。色や文字サイズはお好みでご変更下さい。

/*
Zarigani Design Office Drawer Menu
Copyright 2018 Zarigani Design Office
Customized by minimalgreen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

.drawer_menu a {
	color: inherit;
	text-decoration: none;
}

.drawer_menu a:visited {
	color: inherit;
}


/* PC用ナビゲーション */

.drawer_menu .drawer_nav_wrapper {
	transform: translate(0);
	width: 100%;
	height: 50px;
	/* PC用メニュー高さ */
	position: relative;
	top: auto;
	right: auto;
	z-index: 100;
	background-color: #555;
	/* PC用メニュー背景色 */
}

.drawer_menu .drawer_nav {
	max-width: 1000px;
	/* コンテンツ幅に合わせる */
	padding: 0;
	margin: 0 auto;
	display: -webkit-flex;
	display: flex;
	-webkit-justify-content: center;
	justify-content: center;
	list-style-type: none;
}

.drawer_menu .drawer_nav li {
	font-size: 16px;
	margin: 0;
	padding: 0;
	width: 100%;
	height: 50px;
	/* PC用メニュー高さ */
	line-height: 50px;
	/* PC用メニュー高さ */
	background-color: #555;
	/* PC用メニューボタン背景色 */
	text-align: center;
	list-style-type: none;
}

.drawer_menu .drawer_nav li:hover {
	background-color: #999;
	/* PC用メニューボタンマウスオーバー背景色 */
}

.drawer_menu .drawer_nav li a {
	color: #fff;
	/* PC用メニューボタン文字色 */
}


/* PC非表示 */

@media screen and (min-width:1001px) {
	.pc-hidden {
		display: none;
		text-align: center;
	}
}

@media screen and (min-width: 1001px) {

	/* ドロップダウンメニュー2階層目 */
	.drawer_menu .drawer_nav li ul.second-level {
		visibility: hidden;
		list-style-type: none;
		position: absolute;
		z-index: -1;
		top: 0;
		margin: 0;
		padding-left: 0;
	}

	.drawer_menu .drawer_nav li:hover ul.second-level {
		visibility: visible;
		z-index: 1;
		top: 50px;
		/* PC用メニューの高さに合わせる */
		transition: all .3s;
	}

	.drawer_menu .drawer_nav li:hover ul.second-level li {
		width: 200px;
		/* ドロップダウンメニューボタン横幅 */
		height: 50px;
		/* Pドロップダウンメニューボタン高さ */
		text-align: center;
	}

	.drawer_menu .drawer_nav li:hover ul.second-level li a {
		display: block;
		background-color: #555;
		/* ドロップダウンメニューの文字の背景色 */
		color: #fff;
		/* ドロップダウンメニューの文字色 */
		font-size: 100%;
		text-decoration: none;
		line-height: 50px;
		/* Pドロップダウンメニューボタン高さ */
		text-align: center;
	}

	.drawer_menu .drawer_nav li:hover ul.second-level li a:hover {
		background-color: #999;
		/* ドロップダウンメニューマウスホバー背景色 */
	}

	.touch-btn {
		color: #fff;
		margin-left: 5px;
	}
}

/*+++ Default Button Color +++*/

.drawer_menu .drawer_button {
	color: #fff;
	/* ハンバーガーメニュー文字色 */
	display: none;
}

.drawer_menu .drawer_button .drawer_bar {
	background-color: #fff;
	/* ハンバーガーメニュー三本線の色 */
}


/* 1000px以下 */

@media screen and (max-width: 1000px) {
	.drawer_menu .drawer_bg {
		width: 100%;
		height: 100%;
		position: fixed;
		z-index: 999;
		background-color: rgba(51, 51, 51, 0.5);
		display: none;
		top: 0;
		left: 0;
	}

	.drawer_menu .drawer_button {
		display: block;
		background-color: #555;
		color: #fff;
		border: none;
		padding: 5px;
		width: 50px;
		letter-spacing: 0.1em;
		cursor: pointer;
		position: fixed;
		top: 10px;
		right: 10px;
		z-index: 1001;
		text-align: center;
		outline: none;
	}

	.drawer_menu .drawer_button.active .drawer_bar {
		width: 40px;
	}

	.drawer_menu .drawer_button.active .drawer_bar1 {
		transform: rotate(30deg);
	}

	.drawer_menu .drawer_button.active .drawer_bar2 {
		opacity: 0;
	}

	.drawer_menu .drawer_button.active .drawer_bar3 {
		transform: rotate(-30deg);
	}

	.drawer_menu .drawer_button.active .drawer_menu_text {
		display: none;
	}

	.drawer_menu .drawer_button.active .drawer_close {
		display: block;
	}

	.drawer_menu .drawer_bar {
		display: block;
		height: 2px;
		margin: 8px 2px;
		transition: all 0.2s;
		transform-origin: 0 0;
	}

	.drawer_menu .drawer_text {
		text-align: center;
		font-size: 10px;
	}

	.drawer_menu .drawer_close {
		letter-spacing: 0.08em;
		display: none;
	}

	.drawer_menu .drawer_menu_text {
		display: block;
	}

	.drawer_menu .drawer_nav_wrapper {
		width: 250px;
		height: 100%;
		transition: all 0.2s;
		transform: translate(250px);
		position: fixed;
		top: 0;
		right: 0;
		z-index: 1000;
		background-color: #FFF;
		/* ドロワーメニュー内背景色 */
		overflow-x: hidden;
		overflow-y: auto;
	}

	.drawer_menu .drawer_nav {
		display: block;
		position: relative;
		margin-top: 40px;
	}

	.drawer_menu .drawer_nav li {
		background-color: #fff;
		height: auto;
		line-height: 50px;
		/*  ドロワーメニューリスト項目高さ */
		position: relative;
		border-bottom: 1px solid #eee;
	}

	.drawer_menu .drawer_nav li a {
		background-color: #fff;
		/* ドロワーメニューリスト背景色 */
		color: #555;
		/* ドロワーメニューリスト文字色 */
		display: block;
		text-align: left;
		padding-left: 20px;
	}

	/* スマートフォン2階層目 */
	.drawer_menu .drawer_nav li:hover ul.second-level {
		display: block;
	}

	.drawer_menu .drawer_nav li ul.second-level {
		display: none;
		position: relative;
		padding: 0;
		z-index: 1001;
	}

	.drawer_menu .drawer_nav li ul.second-level li a {
		padding-left: 40px;
	}

	.drawer_menu .drawer_nav_wrapper.open {
		transform: translate(0);
	}

	.drawer_menu.left .drawer_button {
		right: auto;
		left: 32px;
	}

	.drawer_menu.left .drawer_nav_wrapper {
		transform: translate(-250px);
		right: auto;
		left: 0;
	}

	.drawer_menu.left .drawer_nav_wrapper.open {
		transform: translate(0);
	}

	.pc-hidden {
		text-align: center;
	}

	.touch-btn {
		color: #555;
		position: absolute;
		top: .25rem;
		right: 2%;
		display: block;
		z-index: 10;
		width: 50px;
		height: 50px;
		text-align: center;
		vertical-align: middle;
	}
}

/*  ウィジェットタイトル */
.drawer-title {
	text-align: center;
	width: 100%;
	height: 50px;
	line-height: 50px;
	background-color: #555;
	/* ドロワーメニュー内タイトルライン背景色 */
	color: #fff;
}

/* フォローボタン */

.follow-buttons {
	text-align: center;
	margin-top: 0;
	width: 100%;
}

.follow-buttons a {
	display: inline-block;
	width: 60px;
	text-align: center;
	text-decoration: none;
	margin: 5px;
}

.follow-buttons .inner-text {
	font-size: 10px;
	display: block;
}

.follow-buttons .lg,
.content-inner-follow-buttons .fa {
	padding: 15px;
	border-radius: 5px;
	margin: 2px auto;
}

.follow-buttons .hatena {
	color: #38393C;
	background-color: transparent;
}

.follow-buttons .blogicon-hatenablog {
	background: #38393C;
	color: #ffffff;
}

.follow-buttons .blogicon-hatenablog:hover {
	background: #5F6063;
}

.follow-buttons .facebook {
	color: #305097;
	background-color: transparent;
}

.follow-buttons .blogicon-facebook {
	background: #305097;
	color: #ffffff;
}

.follow-buttons .blogicon-facebook:hover {
	background: #5A77B7;
}

.follow-buttons .twitter {
	color: #55acee;
	background-color: transparent;
}

.follow-buttons .blogicon-twitter {
	background: #55acee;
	color: #ffffff;
}

.follow-buttons .blogicon-twitter:hover {
	background: #89C7F7;
}

.follow-buttons .instagram {
	color: #D93177;
	background-color: transparent;
}

.follow-buttons .blogicon-instagram {
	background: #D93177;
	color: #ffffff;
}

.follow-buttons .blogicon-instagram:hover {
	background: #df528d;
}

.follow-buttons .feedly {
	color: #6cc655;
	background-color: transparent;
}

.follow-buttons .blogicon-rss {
	background: #6cc655;
	color: #ffffff;
}

.pc-hidden .profile-icon {
	float: none;
}


まとめ

いかがでしたか?(まとめブログ風)
スマホでもたくさんのメニュー項目を入れたい人は是非ご利用下さい。
但し、デメリットもあります。隠れているメニューはクリック・タップされにくいという点です。

そもそも論になりますが、ユーザーは人目に見えない項目を認識することは困難です。最近はハンバーガーメニューやアイコンの意味を理解している人が増えてきたと思いますが、それでもそのアイコンが何を意味しているかわからないユーザーも多くいらっしゃいます。
f:id:minimalgreen:20190816000147p:plain
三本線や下向き矢印アイコンをタップするとメニューが出てくるUIはユーザーの経験則に依存しています。
この点も考慮した上で、重要な項目は1階層目に持ってくる、すぐ見える所に配置するように工夫することをオススメします。

以上、スマホ対応2階層メニューのご紹介でした。お役に立ちましたら幸いです。

参考リンク:
zarigani-design-office.com