Created
October 15, 2021 09:06
-
-
Save alt-karate/2817817f771bb7301ab1f58fb29dac50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(function() { | |
let tabs = $(".tab"); // tabのクラスを全て取得し、変数tabsに配列で定義 | |
$(".tab").on("click", function() { // tabをクリックしたらイベント発火 | |
$(".active").removeClass("active"); // activeクラスを消す | |
$(this).addClass("active"); // クリックした箇所にactiveクラスを追加 | |
const index = tabs.index(this); // クリックした箇所がタブの何番目か判定し、定数indexとして定義 | |
$(".content").removeClass("show").eq(index).addClass("show"); // showクラスを消して、contentクラスのindex番目にshowクラスを追加 | |
}) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="tab-area"> | |
<div class="tab active"> | |
タブ1 | |
</div> | |
<div class="tab"> | |
タブ2 | |
</div> | |
<div class="tab"> | |
タブ3 | |
</div> | |
<div class="tab"> | |
タブ4 | |
</div> | |
<div class="tab"> | |
タブ5 | |
</div> | |
</div> | |
<div class="content-area"> | |
<div class="content show"> | |
タブ1の中身です | |
</div> | |
<div class="content"> | |
タブ2の中身です | |
</div> | |
<div class="content"> | |
タブ3の中身です | |
</div> | |
<div class="content"> | |
タブ4の中身です | |
</div> | |
<div class="content"> | |
タブ5の中身です | |
</div> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.tab-area { | |
display: flex; | |
justify-content: space-around; | |
background-color: #222e3e; | |
cursor: pointer; | |
} | |
.tab-area .tab { | |
width: 300px; | |
height: 30px; | |
line-height: 30px; | |
text-align: center; | |
color: white; | |
border-right: 1px solid #50637b; | |
border-left: 1px solid #222e3e; | |
} | |
.tab-area .tab.active { | |
background-color: #ccc; | |
color: #222e3e; | |
border: none; | |
} | |
.content-area { | |
font-size: 30px; | |
text-align: center; | |
} | |
.content-area .content { | |
display: none; | |
} | |
.content-area .content.show { | |
margin-top: 50px; | |
display: block; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment