タブメニュー:スライドインジケーター|Tab Menu: Slide Indicator

  • 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">
    <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
.tab-container ul {
    display: flex;
    position: relative;
    margin: 0;
    padding: 4px;
    list-style: none;
    border-radius: 8px;
}

.tab-container .slide-indicator {
    position: absolute;
    top: 4px;
    left: 4px;
    width: calc(33.333% - 8px);
    height: calc(100% - 8px);
    background: #f0f0f0;
    border-radius: 6px;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 1;
}

.tab-container ul li {
    flex: 1;
    position: relative;
    text-align: center;
    margin: 0 1px;
    padding: 16px 24px;
    color: #999;
    font-weight: 300;
    letter-spacing: 0.5px;
    background: transparent;
    border-radius: 6px;
    cursor: pointer;
    z-index: 2;
}

.tab-container ul li.selected {
    color: #333;
    font-weight: 500;
}

.tab-container ul li:not(.selected):hover {
    color: #666;
    background: transparent;
}

.tab-content {
    display: none;
    padding: 32px 24px;
    min-height: 150px;
    color: #666;
    line-height: 1.6;
}

.tab-content.selected {
    display: block;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}
JavaScript
(function() {
const container = document.querySelector('.tab-container');
if (!container) return;
const tabs = container.querySelectorAll('ul li');
const contents = container.querySelectorAll('.tab-content');
const indicator = container.querySelector('.slide-indicator');

tabs.forEach((tab, index) => {
    tab.addEventListener('click', () => {
        tabs.forEach(t => t.classList.remove('selected'));
        tab.classList.add('selected');
        contents.forEach(c => c.classList.remove('selected'));
        const target = document.getElementById(tab.dataset.id);
        if (target) target.classList.add('selected');
        indicator.style.transform = `translateX(${index * 100}%)`;
    });
});
})();
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!
目次