forked from AnujShrivastava01/AnimateItNow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
314 lines (275 loc) Β· 9.77 KB
/
script.js
File metadata and controls
314 lines (275 loc) Β· 9.77 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// π Function for displaying FAQ categories
function displaycategory(category){
const general=document.getElementById('general-faq');
const technical=document.getElementById('technical-faq');
if(category==='general'){
general.style.display='block';
technical.style.display='none';
}
else if(category==='technical'){
general.style.display='none';
technical.style.display='block';
}
}
// π§Ή Service worker registration removed to fix 404 error
// if ('serviceWorker' in navigator) {
// window.addEventListener('load', () => {
// navigator.serviceWorker.register('/sw.js').then((registration) => {
// console.log('Service Worker registered:', registration);
//
// registration.onupdatefound = () => {
// const newWorker = registration.installing;
// newWorker.onstatechange = () => {
// if (
// newWorker.state === 'installed' &&
// navigator.serviceWorker.controller
// ) {
// console.log('New version available. Reloading...');
// window.location.reload();
// }
// };
// };
// }).catch((error) => {
// console.error('Service Worker registration failed:', error);
// });
// });
// }
// β¨ Enhanced typewriter effect with improved performance
function typewriter(){
const el=document.getElementById("modify");
if(!el)return;
const text=el.textContent;
el.textContent='';
let index=0;
let interval=setInterval(()=>{
if(index<text.length){
el.textContent+=text.charAt(index);
index++;
}
else{
clearInterval(interval);
}
},100);
}
// π Initialize typewriter effect
typewriter();
// π§ Function to make the FAQ collapsible with enhanced accessibility
function toggleFAQ(element) {
// Ensure we have a valid element
if (!element || !document.querySelector(".faq-item")) return;
// Find the closest FAQ item container
const faqItem = element.closest(".faq-item");
if (!faqItem) return;
// Toggle the active class on this specific FAQ item
const isActive = faqItem.classList.contains("active");
faqItem.classList.toggle("active");
// Update aria-expanded attribute for accessibility
const button = faqItem;
const answerId = button.getAttribute("aria-controls");
const answer = document.getElementById(answerId);
if (answer) {
button.setAttribute("aria-expanded", !isActive);
answer.hidden = isActive;
}
}
// π Make toggleFAQ globally accessible
window.toggleFAQ = toggleFAQ;
// β¨οΈ Add keyboard support for FAQ items
document.addEventListener('DOMContentLoaded', function() {
const faqItems = document.querySelectorAll('.faq-item');
faqItems.forEach(item => {
// Add click event
item.addEventListener('click', function() {
toggleFAQ(this);
});
// Add keyboard event
item.addEventListener('keydown', function(e) {
if (e.key === 'Enter' || e.key === ' ') {
e.preventDefault();
toggleFAQ(this);
}
});
});
});
// π Global (or module-level) variables to store animation and listener references for snake cursor
let currentAnimationId = null
let currentMousemoveListener = null
let snakeContainerElement = null // Keep a reference to the container element
// π¦ Removed the problematic 'const lucide = { createIcons: () => {}, }' declaration.
// The actual 'lucide' object is provided by the external script loaded in HTML.
// Declaring it as 'const' here prevented the global 'lucide' from being used.
// ποΈ Moved cursor functions outside DOMContentLoaded for better scope and reusability
const isMobile = window.matchMedia("(max-width: 768px)").matches
// π¨ Enhanced snake cursor with improved performance
function enableSnakeCursor() {
// Always ensure a clean slate before enabling.
// This is crucial for persistence across page navigations (especially with bfcache)
disableSnakeCursor()
snakeContainerElement = document.createElement("div") // Assign to global variable
snakeContainerElement.id = "cursor-snake"
document.body.appendChild(snakeContainerElement)
const dots = []
const dotCount = 20
for (let i = 0; i < dotCount; i++) {
const dot = document.createElement("div")
dot.className = "snake-dot"
snakeContainerElement.appendChild(dot) // Append to the new global container
dots.push({ el: dot, x: 0, y: 0 })
}
let mouseX = window.innerWidth / 2
let mouseY = window.innerHeight / 2
// Store event listener reference in a global variable
currentMousemoveListener = (e) => {
mouseX = e.clientX
mouseY = e.clientY
}
document.addEventListener("mousemove", currentMousemoveListener)
// π Animate snake with optimized performance
function animateSnake() {
let x = mouseX,
y = mouseY
dots.forEach((dot, i) => {
dot.x += (x - dot.x) * 0.2
dot.y += (y - dot.y) * 0.2
dot.el.style.left = dot.x + "px"
dot.el.style.top = dot.y + "px"
dot.el.style.transform = `scale(${1 - i / dotCount})`
x = dot.x
y = dot.y
})
// Store the animation ID in a global variable
currentAnimationId = requestAnimationFrame(animateSnake)
}
animateSnake()
}
// π Disable snake cursor with enhanced cleanup
function disableSnakeCursor() {
// Use the global reference to the container element
if (snakeContainerElement) {
if (currentAnimationId) {
cancelAnimationFrame(currentAnimationId)
currentAnimationId = null // Reset global ID
}
if (currentMousemoveListener) {
document.removeEventListener("mousemove", currentMousemoveListener)
currentMousemoveListener = null // Reset global listener
}
snakeContainerElement.remove() // Remove the cursor container
snakeContainerElement = null // Reset global reference
}
}
// π Add event listener for page unload to ensure cleanup, especially for bfcache
window.addEventListener("pagehide", () => {
disableSnakeCursor()
})
// π Theme toggle with enhanced functionality
window.addEventListener("DOMContentLoaded", () => {
// Theme toggle
const themeToggle = document.getElementById("theme-toggle")
const body = document.body
// π¨ Set theme with improved icon handling
function setTheme(dark) {
const newIcon = dark ? "sun" : "moon"
body.classList.toggle("dark", dark) // Use 'dark' class for consistency
localStorage.setItem("theme", dark ? "dark" : "light")
// Replace icon completely
if (themeToggle) {
themeToggle.innerHTML = `<i data-lucide="${newIcon}"></i>`
// Only call lucide.createIcons() if the lucide object is actually available
if (window.lucide) {
window.lucide.createIcons()
}
}
}
// π§ Load saved theme with fallback
const savedTheme = localStorage.getItem("theme")
setTheme(savedTheme === "dark")
// π±οΈ Add theme toggle event listener
if (themeToggle) {
themeToggle.addEventListener("click", () => {
const isDark = body.classList.contains("dark")
setTheme(!isDark)
})
}
})
// π± Mobile menu toggle functionality
document.addEventListener('DOMContentLoaded', function() {
const menuToggle = document.getElementById('menu-toggle');
const navMenu = document.querySelector('.nav-menu');
if (menuToggle && navMenu) {
menuToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
menuToggle.classList.toggle('active');
});
// π±οΈ Close menu when clicking outside
document.addEventListener('click', function(e) {
if (!menuToggle.contains(e.target) && !navMenu.contains(e.target)) {
navMenu.classList.remove('active');
menuToggle.classList.remove('active');
}
});
}
});
// π Enhanced scroll progress indicator
function updateScrollProgress() {
const scrollTop = window.scrollY;
const docHeight = document.body.scrollHeight - window.innerHeight;
const progress = (scrollTop / docHeight) * 100;
const progressBar = document.getElementById('scroll-progress');
if (progressBar) {
progressBar.style.width = progress + '%';
}
}
// π Initialize scroll progress tracking
document.addEventListener('scroll', updateScrollProgress);
// π― Smooth scroll to anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
// π Copy to clipboard functionality
function copyToClipboard(text) {
// π Try navigator clipboard API first
if (navigator.clipboard) {
navigator.clipboard.writeText(text).then(() => {
// π Success callback
console.log('Text copied to clipboard');
}).catch(err => {
// β οΈ Error callback
console.error('Failed to copy text: ', err);
});
} else {
// π Fallback for older browsers
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
}
}
// π§ͺ Console log for debugging purposes
console.log("β¨ Script initialized successfully");
// π Performance monitoring placeholder
function logPerformance() {
// π This is a placeholder for future performance monitoring
// console.log("Performance metrics:", performance.memory);
}
// π§Ό Cleanup function for memory management
function cleanup() {
// π§Ή This is a placeholder for future cleanup operations
console.log("π§Ή Cleanup completed");
}
// π Initialize all components when DOM is fully loaded
document.addEventListener('DOMContentLoaded', function() {
// π¬ All initialization code goes here
console.log("π¬ All components initialized");
});