-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.js
More file actions
239 lines (208 loc) · 7.66 KB
/
template.js
File metadata and controls
239 lines (208 loc) · 7.66 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Navigation Bar Functionality
document.addEventListener('DOMContentLoaded', function() {
// Get DOM elements
const navbar = document.querySelector('.navbar');
const navMenu = document.querySelector('.nav-menu');
const navLinks = document.querySelectorAll('.nav-menu a');
const mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
// Create mobile menu toggle button if it doesn't exist
if (!mobileMenuToggle && window.innerWidth <= 768) {
createMobileMenuToggle();
}
// Mobile Menu Toggle Functionality
function createMobileMenuToggle() {
const toggleButton = document.createElement('button');
toggleButton.className = 'mobile-menu-toggle';
toggleButton.innerHTML = '☰';
toggleButton.style.cssText = `
display: none;
background: none;
border: none;
color: white;
font-size: 1.5rem;
cursor: pointer;
padding: 10px;
`;
navbar.appendChild(toggleButton);
// Add mobile menu styles
const style = document.createElement('style');
style.textContent = `
@media (max-width: 768px) {
.mobile-menu-toggle {
display: block !important;
}
.nav-menu {
display: none;
position: absolute;
top: 100%;
left: 0;
width: 100%;
background-color: #2c3e50;
flex-direction: column;
padding: 1rem 0;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
.nav-menu.active {
display: flex !important;
}
.nav-menu li {
margin: 0.5rem 0;
text-align: center;
}
}
`;
document.head.appendChild(style);
// Toggle mobile menu
toggleButton.addEventListener('click', function() {
navMenu.classList.toggle('active');
toggleButton.innerHTML = navMenu.classList.contains('active') ? '✕' : '☰';
});
// Close mobile menu when clicking outside
document.addEventListener('click', function(e) {
if (!navbar.contains(e.target)) {
navMenu.classList.remove('active');
toggleButton.innerHTML = '☰';
}
});
}
// Smooth Scrolling for Navigation Links
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetSection = document.querySelector(targetId);
if (targetSection) {
// Calculate offset for fixed header
const headerHeight = navbar.offsetHeight;
const targetPosition = targetSection.offsetTop - headerHeight;
// Smooth scroll to target
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
// Close mobile menu if open
const mobileMenu = document.querySelector('.mobile-menu-toggle');
if (mobileMenu && navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
mobileMenu.innerHTML = '☰';
}
}
});
});
// Active Link Highlighting based on Scroll Position
function updateActiveLink() {
const sections = document.querySelectorAll('section[id]');
const headerHeight = navbar.offsetHeight;
const scrollPosition = window.scrollY + headerHeight + 100; // Offset for better detection
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.offsetHeight;
const sectionId = '#' + section.getAttribute('id');
// Check if current scroll position is within this section
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
// Remove active class from all links
navLinks.forEach(link => link.classList.remove('active'));
// Add active class to current section link
const activeLink = document.querySelector(`.nav-menu a[href="${sectionId}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
}
});
}
// Navbar Background Change on Scroll
function updateNavbarBackground() {
const scrollPosition = window.scrollY;
if (scrollPosition > 50) {
navbar.style.backgroundColor = 'rgba(44, 62, 80, 0.95)';
navbar.style.backdropFilter = 'blur(10px)';
} else {
navbar.style.backgroundColor = '#2c3e50';
navbar.style.backdropFilter = 'none';
}
}
// Add active link styles
const style = document.createElement('style');
style.textContent = `
.nav-menu a.active {
color: #4CAF50 !important;
font-weight: bold;
}
.nav-menu a:hover {
color: #4CAF50;
}
`;
document.head.appendChild(style);
// Scroll Event Listeners
let ticking = false;
function handleScroll() {
if (!ticking) {
requestAnimationFrame(() => {
updateActiveLink();
updateNavbarBackground();
ticking = false;
});
ticking = true;
}
}
window.addEventListener('scroll', handleScroll);
// Handle window resize for mobile menu
window.addEventListener('resize', function() {
const mobileMenu = document.querySelector('.mobile-menu-toggle');
if (window.innerWidth > 768) {
navMenu.classList.remove('active');
if (mobileMenu) {
mobileMenu.innerHTML = '☰';
}
}
});
// Initialize on page load
updateActiveLink();
updateNavbarBackground();
// Add loading animation for smooth transitions
navbar.style.transition = 'all 0.3s ease';
});
// Utility function to scroll to top
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
// Add scroll to top button functionality (optional)
function createScrollToTopButton() {
const scrollButton = document.createElement('button');
scrollButton.innerHTML = '↑';
scrollButton.className = 'scroll-to-top';
scrollButton.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: #4CAF50;
color: white;
border: none;
font-size: 1.5rem;
cursor: pointer;
display: none;
z-index: 1000;
transition: all 0.3s ease;
`;
document.body.appendChild(scrollButton);
// Show/hide scroll to top button
window.addEventListener('scroll', function() {
if (window.scrollY > 300) {
scrollButton.style.display = 'block';
} else {
scrollButton.style.display = 'none';
}
});
// Scroll to top when clicked
scrollButton.addEventListener('click', scrollToTop);
}
// Initialize scroll to top button
document.addEventListener('DOMContentLoaded', function() {
createScrollToTopButton();
});