Lesson 17 of 18
Portfolio Project — Part 2 ⚙
What You'll Learn
- Add JavaScript interactivity to your portfolio
- Implement smooth scrolling and a mobile menu
- Animate elements on scroll
Smooth Scroll Navigation
CSS
/* In CSS */
html { scroll-behavior: smooth; }JavaScript
// Highlight active nav link based on scroll position
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('.nav-links a');
window.addEventListener('scroll', () => {
let current = '';
sections.forEach(section => {
if (window.scrollY >= section.offsetTop - 100) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === '#' + current) {
link.classList.add('active');
}
});
});Mobile Hamburger Menu
JavaScript
const menuBtn = document.getElementById('menu-btn');
const navLinks = document.getElementById('nav-links');
menuBtn.addEventListener('click', () => {
navLinks.classList.toggle('open');
menuBtn.textContent = navLinks.classList.contains('open') ? 'X' : '☰';
});CSS
.nav-links { display: none; }
.nav-links.open { display: flex; flex-direction: column; }
@media (min-width: 768px) {
.nav-links { display: flex; }
#menu-btn { display: none; }
}Scroll Animations
JavaScript
// Animate elements as they enter the viewport
const observer = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
}, { threshold: 0.1 }
);
document.querySelectorAll('.fade-in').forEach(el => {
observer.observe(el);
});CSS
.fade-in { opacity: 0; transform: translateY(30px); transition: all 0.7s ease; }
.fade-in.visible { opacity: 1; transform: translateY(0); }Polish Your Portfolio
Add all three JavaScript features above to your portfolio. Then finalize: fill in all your real projects and skills, add your actual contact info, double-check on mobile. Your portfolio should be ready to go live!
Quick Check
What CSS property enables smooth scrolling on the whole page?
What does IntersectionObserver do?
How do you toggle a CSS class in JavaScript?