-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
210 lines (174 loc) · 7.13 KB
/
script.js
File metadata and controls
210 lines (174 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
document.addEventListener('DOMContentLoaded', () => {
// 0. Force scroll to top on refresh
if ('scrollRestoration' in history) {
history.scrollRestoration = 'manual';
}
window.scrollTo(0, 0);
// 1. Set Current Year in Footer
const yearElement = document.getElementById('currentYear');
if (yearElement) {
yearElement.textContent = new Date().getFullYear();
}
// 2. Theme Toggle Logic
const themeToggleBtn = document.getElementById('themeToggle');
const htmlElement = document.documentElement;
// Check for saved theme preference or use system preference
const savedTheme = localStorage.getItem('theme');
const systemPrefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (savedTheme) {
htmlElement.setAttribute('data-theme', savedTheme);
} else if (systemPrefersDark) {
htmlElement.setAttribute('data-theme', 'dark');
} else {
htmlElement.setAttribute('data-theme', 'light');
}
if (themeToggleBtn) {
themeToggleBtn.addEventListener('click', () => {
const currentTheme = htmlElement.getAttribute('data-theme');
const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
htmlElement.setAttribute('data-theme', newTheme);
localStorage.setItem('theme', newTheme);
});
}
// 3. Scroll Progress Indicator
const scrollProgress = document.getElementById('scrollProgress');
const updateScrollProgress = () => {
const scrollTop = window.scrollY || document.documentElement.scrollTop;
const scrollHeight = document.documentElement.scrollHeight - document.documentElement.clientHeight;
const scrollPercentage = (scrollTop / scrollHeight) * 100;
if (scrollProgress) {
scrollProgress.style.width = `${scrollPercentage}%`;
}
};
window.addEventListener('scroll', updateScrollProgress);
// 4. Navbar Sticky State & Active Link Update
const navbar = document.querySelector('.navbar');
const sections = document.querySelectorAll('section');
const navLinks = document.querySelectorAll('.nav-link');
const handleScroll = () => {
// Sticky Navbar styling
if (navbar) {
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
}
// Active Link Highlighting
let currentSection = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (window.scrollY >= (sectionTop - 200)) {
currentSection = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${currentSection}`) {
link.classList.add('active');
}
});
};
window.addEventListener('scroll', handleScroll);
// 5. Intersection Observer for Scroll Animations (Fade-in, Slide-up, etc.)
const animatedElements = document.querySelectorAll('.fade-in, .fade-in-up, .fade-in-left, .fade-in-right');
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.15
};
const animationObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
// Apply animation class when intersecting, remove it when not
if (entry.isIntersecting) {
entry.target.classList.add('appear');
} else {
entry.target.classList.remove('appear');
}
});
}, observerOptions);
animatedElements.forEach(el => {
animationObserver.observe(el);
});
// 6. Back to Top Button
const backToTopBtn = document.getElementById('backToTop');
if (backToTopBtn) {
backToTopBtn.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
}
// 7. Contact Form Handling (Styling Only)
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', () => {
const btn = contactForm.querySelector('button[type="submit"]');
btn.innerHTML = 'Sending... <i class="fa-solid fa-spinner fa-spin ml-2"></i>';
btn.disabled = true;
});
}
// 8. Parallax Scrolling Effect
const parallaxElements = document.querySelectorAll('.parallax');
const handleParallax = () => {
const scrolled = window.scrollY;
parallaxElements.forEach(el => {
const speed = el.getAttribute('data-speed') || 0.5;
// Calculate translateY offset. Using requestAnimationFrame for performance.
const yPos = scrolled * speed;
el.style.transform = `translateY(${yPos}px)`;
});
};
window.addEventListener('scroll', () => {
window.requestAnimationFrame(handleParallax);
});
// 9. Horizontal Parallax for Projects Carousel
const projectsGrid = document.querySelector('.projects-grid');
const projectCards = document.querySelectorAll('.project-card');
if (projectsGrid) {
const handleHorizontalParallax = () => {
projectCards.forEach(card => {
const img = card.querySelector('.project-image');
if (img && !img.classList.contains('app-icon')) {
const rect = card.getBoundingClientRect();
const cardCenter = rect.left + (rect.width / 2);
const windowCenter = window.innerWidth / 2;
const distance = cardCenter - windowCenter;
const shift = distance * 0.15;
img.style.transform = `translateX(${shift}px)`;
}
});
};
projectsGrid.addEventListener('scroll', () => {
window.requestAnimationFrame(handleHorizontalParallax);
});
// Drag to Scroll
let isDown = false;
let startX;
let scrollLeft;
projectsGrid.addEventListener('mousedown', (e) => {
isDown = true;
startX = e.pageX - projectsGrid.offsetLeft;
scrollLeft = projectsGrid.scrollLeft;
projectsGrid.style.scrollBehavior = 'auto'; // Disable smooth scroll while dragging
});
projectsGrid.addEventListener('mouseleave', () => {
isDown = false;
});
projectsGrid.addEventListener('mouseup', () => {
isDown = false;
projectsGrid.style.scrollBehavior = 'smooth'; // Re-enable smooth scroll
});
projectsGrid.addEventListener('mousemove', (e) => {
if (!isDown) return;
e.preventDefault();
const x = e.pageX - projectsGrid.offsetLeft;
const walk = (x - startX) * 2; // scroll-fast
projectsGrid.scrollLeft = scrollLeft - walk;
});
// Initial setup
handleHorizontalParallax();
}
});