- 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 - Slide Button</h1>
<div class="tab-container">
<ul>
<div class="slide-indicator"></div>
<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 {
margin: 0 auto;
border-radius: 12px;
padding: 20px;
max-width: 600px;
width: 100%;
}
.tab-container {
position: relative;
}
.tab-container ul {
margin: 0;
padding: 0;
list-style: none;
display: flex;
background: #eeeeee;
border-radius: 48px;
padding: 6px;
position: relative;
}
.slide-indicator {
position: absolute;
top: 6px;
left: 6px;
width: calc(33.333% - 4px);
height: calc(100% - 12px);
background: linear-gradient(135deg, #4a4a4a 0%, #2c2c2c 100%);
border-radius: 48px;
transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
z-index: 1;
}
.tab-container ul li {
flex: 1;
padding: 14px 18px;
text-align: center;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
z-index: 2;
font-weight: 500;
font-size: 14px;
color: #333333;
border-radius: 8px;
margin: 0 2px;
background: transparent;
}
.tab-container ul li.selected {
color: #ffffff;
font-weight: 600;
text-shadow: 0 1px 2px rgba(0, 0, 0, 0.3);
}
.tab-container ul li:not(.selected):hover {
color: #aaaaaa;
background: rgba(255, 255, 255, 0.1);
transform: translateY(-1px);
}
.tab-content {
display: none;
padding: 24px;
min-height: 150px;
background: #fefefe;
border: 1px solid #e0e0e0;
border-radius: 8px;
margin-top: 16px;
}
.tab-content.selected {
display: block;
animation: slideContentIn 0.3s ease-out;
}
@keyframes slideContentIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
h1 {
text-align: center;
color: #333;
margin-bottom: 30px;
font-size: 24px;
font-weight: 600;
}
JavaScript
const tabContainer = document.querySelector('.tab-container');
const tabMenuItems = tabContainer.querySelectorAll('ul li');
const tabContents = tabContainer.querySelectorAll('.tab-content');
const slideIndicator = tabContainer.querySelector('.slide-indicator');
tabMenuItems.forEach((tabMenuItem, index) => {
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');
// スライドインジケーターの位置を更新
slideIndicator.style.transform = `translateX(${index * 100}%)`;
});
});