-
Tab navigation animation
HTML
CSS
JavaScript
Home
About
Contact
🏠 Home Content
📄 About Content
📞 Contact Content
* { margin: 0; padding: 0; box-sizing: border-box; font-family: sans-serif; } body { background: #0f172a; color: white; display: flex; justify-content: center; align-items: center; height: 100vh; } /* Container */ .tabs-container { width: 600px; } /* Tabs */ .tabs { display: flex; position: relative; background: #1e293b; border-radius: 12px; padding: 5px; } /* Tab Button */ .tab { flex: 1; padding: 12px; text-align: center; cursor: pointer; position: relative; z-index: 2; transition: 0.3s; } .tab:hover { color: #38bdf8; } /* Active Tab */ .tab.active { color: white; } /* Sliding Indicator */ .indicator { position: absolute; height: 100%; width: 33.33%; background: linear-gradient(135deg, #38bdf8, #6366f1); border-radius: 10px; top: 0; left: 0; transition: transform 0.4s ease; z-index: 1; } /* Content */ .content { margin-top: 20px; padding: 20px; background: #1e293b; border-radius: 12px; overflow: hidden; position: relative; } /* Tab Content */ .tab-content { position: absolute; width: 100%; opacity: 0; transform: translateY(20px); transition: 0.4s ease; } .tab-content.active { position: relative; opacity: 1; transform: translateY(0); }
const tabs = document.querySelectorAll('.tab'); const contents = document.querySelectorAll('.tab-content'); const indicator = document.querySelector('.indicator'); tabs.forEach(tab => { tab.addEventListener('click', () => { // Remove active tabs.forEach(t => t.classList.remove('active')); contents.forEach(c => c.classList.remove('active')); // Add active tab.classList.add('active'); contents[tab.dataset.index].classList.add('active'); // Move indicator indicator.style.transform = `translateX(${tab.dataset.index * 100}%)`; }); });
Live Preview