← 一覧へ

Tab Menu / 15 スライドボタン|Slide Button

デザイン見本

  • 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.

背景のハイライトがスライドして移動する、直感的なアニメーションが特徴のデザインです。アプリのメニュー切り替えのような、シームレスでスムーズな操作感を提供します。モバイルアプリ風のUIに最適です。

実装コード

HTML
<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">
    Tab 1 のコンテンツ
  </div>
  <div class="tab-content" id="tab-2">
    Tab 2 のコンテンツ
  </div>
  <div class="tab-content" id="tab-3">
    Tab 3 のコンテンツ
  </div>
</div>
CSS
.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-container .tab-content {
  display: none;
  padding: 24px;
  min-height: 150px;
  background: #fefefe;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  margin-top: 16px;
}

.tab-container .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); }
}
JS
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', () => {
    tabMenuItems.forEach(item => {
      item.classList.remove('selected');
    });
    tabMenuItem.classList.add('selected');

    tabContents.forEach(tabContent => {
      tabContent.classList.remove('selected');
    });
    document.getElementById(tabMenuItem.dataset.id).classList.add('selected');

    // スライドインジケーターの位置を更新
    slideIndicator.style.transform = `translateX(${index * 100}%)`;
  });
});