4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像を使わずcssだけで、開閉するリストによくある矢印を作る

Last updated at Posted at 2016-02-11

このような縦に並ぶリストで、リストの右端に並んでいる矢印をcssで作る。
スクリーンショット 2016-02-12 0.03.08.png

<ul class="menu">
	<li class="hasChild">
		<a href="#">リンク1</a>
		<ul class="subNav">
			<li><a href="#">リンク1-1</a></li>
			<li><a href="#">リンク1-1</a></li>
			<li><a href="#">リンク1-1</a></li>
		</ul>
	</li>
	<li><a href="#">リンク2</a></li>
	<li><a href="#">リンク3</a></li>
	<li><a href="#">リンク4</a></li>
	<li><a href="#">リンク5</a></li>
</ul>
li {
	height: 40px;
	width: 200px;
	border-color: 1px solid #ccc;
}
li a {
	display: block;
	width: 100%;
	height: 100%;
	position: relative; /* このaタグを基準に矢印を作成 */
}
li a:after {
	content: '';
	position: absolute;
	top: 50%;
	right: 10px;
	display: block;
	width: 8px;
	height: 8px;
	margin-top: -4px;
	border-top: 2px solid #333; /* 線の太さ */
	border-right: 2px solid #333; /* 線の太さ */
	transform: rotate(45deg); /* 右向き矢印 */
}
.subNav {
	display: none;
}
li.hasChild a:after {
	transform: rotate(135deg);
	/* 下向き矢印(サブリストが開じている時) */
}
li.hasChild.open a:after {
	transform: rotate(-45deg);
	/* 上向き矢印(サブリストが開いている時) */
}
$('.hasChild > a').click(function(e){
	e.preventDefault();
	var li = $(this).closest('li.hasChild')
	if(li.hasClass('open')){
		li.removeClass('open').find('.subNav').slideUp('fast');
	}else{
		$('.hasChild').removeClass('open').find('.subNav').slideUp('fast');
		/* 上の行を書くと一つだけしか開いた状態にならない。 */
		setTimeout(function(){ /* 閉じてから開くためにsetTimeout */
			li.addClass('open').find('.subNav').slideDown('fast');					
		},200);
	}
});

4
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?