- Tab 1
- Tab 2
- Tab 3
Good morning. This is the content of Tab 1.
Hello. This is the content of Tab 2.
Good evening. This is the content of Tab 3.
HTML
<div class="container">
<h1>Tab Menu - Vertical Divider Style</h1>
<div class="tab-container">
<ul>
<li class="selected" data-id="tab-1">Tab 1</li>
<li data-id="tab-2">Tab 2</li>
<li data-id="tab-3">Tab 3</li>
</ul>
<div class="tab-content selected" id="tab-1">
Good morning. This is the content of Tab 1.
</div>
<div class="tab-content" id="tab-2">
Hello. This is the content of Tab 2.
</div>
<div class="tab-content" id="tab-3">
Good evening. This is the content of Tab 3.
</div>
</div>
</div>
CSS
.container {
max-width: 800px;
margin: 0 auto;
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
}
.tab-container {
overflow: hidden;
position: relative;
background: #fff;
}
.tab-container ul {
margin: 0 24px;
padding: 16px 24px;
list-style: none;
display: flex;
background: transparent;
position: relative;
border: 1px solid #e0e0e0;
}
.tab-container ul li {
flex: 1;
padding: 8px 16px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
color: #666;
font-weight: 500;
font-family: 'Arial', sans-serif;
background: transparent;
border-right: 1px solid #e0e0e0;
}
.tab-container ul li:last-child {
border-right: none;
}
.tab-container ul li.selected {
color: #333;
font-weight: 600;
}
.tab-container ul li:not(.selected):hover {
background: #eeeeee;
color: #333;
}
.tab-content {
display: none;
padding: 24px;
min-height: 150px;
background: #fff;
color: #333;
position: relative;
}
.tab-content.selected {
display: block;
animation: simpleFadeIn 0.3s ease;
}
@keyframes simpleFadeIn {
from {
opacity: 0;
transform: translateY(5px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
JavaScript
document.addEventListener('DOMContentLoaded', function() {
const tabMenuItems = document.querySelectorAll('.tab-container ul li');
const tabContents = document.querySelectorAll('.tab-content');
tabMenuItems.forEach(tabMenuItem => {
tabMenuItem.addEventListener('click', () => {
// 全てのタブからselectedクラスを外す
tabMenuItems.forEach(item => {
item.classList.remove('selected');
});
// クリックされたタブのみselectedクラスを付ける
tabMenuItem.classList.add('selected');
// 全てのタブのコンテンツからselectedクラスを外す
tabContents.forEach(tabContent => {
tabContent.classList.remove('selected');
});
// クリックされたタブのカスタムデータ属性と同じIDを持つコンテンツに、selectedクラスを付ける
document.getElementById(tabMenuItem.dataset.id).classList.add('selected');
});
});
});