-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
360 lines (305 loc) · 10.5 KB
/
script.js
File metadata and controls
360 lines (305 loc) · 10.5 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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Custom cursor
inputBox.focus();
const cursor = document.querySelectorAll(".cursor");
let big_cursor = document.querySelector(".big");
let small_cursor = document.querySelector(".small");
const isTouchDevice = "ontouchstart" in window || navigator.maxTouchPoints > 0;
if (!isTouchDevice) {
document.documentElement.addEventListener("mousemove", (event) => {
if (hasPointerCursor(event.target)) {
big_cursor.classList.add("mouse-down");
small_cursor.classList.add("mouse-down");
} else {
if (big_cursor.classList.contains("mouse-down")) {
big_cursor.classList.remove("mouse-down");
small_cursor.classList.remove("mouse-down");
}
}
// cursor.forEach((item) => (item.style.opacity = hasPointerCursor(event.target)?0:1));
big_cursor.style.top = event.clientY + window.scrollY + "px";
big_cursor.style.left = event.clientX + window.scrollX + "px";
setTimeout(() => {
small_cursor.style.top = event.clientY + window.scrollY + "px";
small_cursor.style.left = event.clientX + window.scrollX + "px";
}, 150);
});
document.documentElement.addEventListener("mouseleave", () => {
cursor.forEach((item) => (item.style.opacity = 0));
});
document.documentElement.addEventListener("mouseenter", () => {
cursor.forEach((item) => (item.style.opacity = 1));
});
} else {
document.body.style.cursor = "auto";
}
document.body.addEventListener("mousedown", () => {
big_cursor.classList.add("mouse-down");
});
document.body.addEventListener("mouseup", () => {
big_cursor.classList.remove("mouse-down");
});
function hasPointerCursor(element) {
while (element) {
if (getComputedStyle(element).cursor === "pointer") {
return true;
}
element = element.parentElement;
}
return false;
}
// code implementation in different languages
const languageSelector = document.querySelector(".language-selector");
const codeContainer = document.querySelector("#code-display");
const copyButton = document.querySelector(".language-copy-button");
document.addEventListener("DOMContentLoaded", function () {
document.querySelector('button[data-language="c"]').click();
if (window.innerWidth < 768) {
document.querySelector('button[data-language="javascript"]').textContent =
"js";
}
const date = new Date();
const yearEl = document.getElementById("year");
if (yearEl) yearEl.textContent = date.getFullYear();
});
languageSelector.addEventListener("click", (event) => {
const selectedLanguage = event.target.dataset.language;
codeContainer.style.display = "block";
codeContainer.textContent = codeSnippets[selectedLanguage];
codeContainer.classList.forEach((className) => {
if (className.startsWith("language-")) {
codeContainer.classList.remove(className);
}
});
codeContainer.classList.add(`language-${selectedLanguage}`);
Prism.highlightElement(codeContainer);
codeContainer.style.animation = "none";
codeContainer.offsetHeight;
codeContainer.style.animation = "fadeInTopToBottom 1s forwards";
});
copyButton.addEventListener("click", () => {
navigator.clipboard.writeText(codeContainer.textContent);
copyButton.textContent = "Copied!";
setTimeout(() => {
copyButton.textContent = "Copy";
}, 5000);
});
// for show hide of how to
let items = document.getElementsByClassName("howto");
let showButtons = document.getElementsByClassName("show");
let hideButtons = document.getElementsByClassName("hide");
for (let i = 0; i < items.length; i++) {
items[i].addEventListener("click", () => {
const ansSection = items[i].querySelector(".ans");
const isActive = items[i].classList.toggle("active");
if (isActive) {
// Measure the full height needed for the answer section
ansSection.style.maxHeight = ansSection.scrollHeight + "px";
showButtons[i].style.display = "none";
hideButtons[i].style.display = "block";
} else {
// Reset the max-height to 0 to collapse
ansSection.style.maxHeight = 0;
showButtons[i].style.display = "block";
hideButtons[i].style.display = "none";
}
});
}
// yaha tak
const navigation = document.querySelector(".nav-links");
const container = document.querySelector(".container");
// navigationLeftReset();
// function navigationLeftReset() {
// const containerRect = container.getBoundingClientRect();
// const containerLeft = containerRect.left;
// navigation.style.marginLeft = `${containerLeft}px`;
// }
// window.addEventListener("resize", navigationLeftReset);
// Custom right click context menu
const contextMenu = document.getElementById("context-menu");
document.addEventListener("contextmenu", (event) => {
event.preventDefault();
contextMenu.style.display = "block";
const x = event.clientX;
const y = event.clientY;
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const scrollY = window.scrollY;
const scrollX = window.scrollX;
const menuWidth = contextMenu.offsetWidth;
const menuHeight = contextMenu.offsetHeight;
let left = x + scrollX;
let top = y + scrollY;
if (left + menuWidth > windowWidth + scrollX) {
left = windowWidth + scrollX - menuWidth;
}
if (top + menuHeight > windowHeight + scrollY) {
top = windowHeight + scrollY - menuHeight;
}
contextMenu.style.left = left + "px";
contextMenu.style.top = top + "px";
const submenus = contextMenu.querySelectorAll(".submenu");
submenus.forEach((submenu) => {
const parentLi = submenu.parentElement;
parentLeft = parentLi.getBoundingClientRect();
submenu.style.top = parentLi.offsetTop + "px";
parentLi.addEventListener("mouseenter", () => {
submenu.style.display = "flex";
const submenuWidth = submenu.offsetWidth;
if (parentLeft.right + submenuWidth > windowWidth + scrollX) {
submenu.style.left = menuWidth - 2 * submenuWidth - 20 + "px";
} else {
submenu.style.left = menuWidth - 2 + "px";
}
});
parentLi.addEventListener("mouseleave", () => {
submenu.style.display = "none";
});
});
});
document.addEventListener("click", () => {
contextMenu.style.display = "none";
});
// end context menu
const navbar = document.querySelector("nav");
let prevScrollPos = window.scrollY;
window.addEventListener("scroll", function () {
const scrollTop = window.scrollY;
const docHeight = document.documentElement.scrollHeight - window.innerHeight;
const scrollPercent = (scrollTop / docHeight) * 100;
document.querySelector(".progress-bar").style.height = scrollPercent + "%";
// for navbar show hide
const currentScrollPos = window.scrollY;
if (prevScrollPos > currentScrollPos) {
navbar.classList.remove("scrolled");
} else {
navbar.classList.add("scrolled");
}
prevScrollPos = currentScrollPos;
});
// back to top
const backToTopBtn = document.getElementById("backToTopBtn");
window.addEventListener("scroll", () => {
if (window.scrollY > 100) {
backToTopBtn.style.display = "flex";
} else {
backToTopBtn.style.display = "none";
}
});
backToTopBtn.addEventListener("click", () => {
window.scrollTo({ top: 0, behavior: "smooth" });
});
// Animation on scroll
const animatedElements = document.querySelectorAll(".scroll-animate");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.classList.add("show-content");
}
//else {
// entry.target.classList.remove("show");
// }
});
},
{
threshold: 0.1,
},
);
animatedElements.forEach((element) => observer.observe(element));
// this much for observer
function copyToClipboard(button) {
const codeBlock = button.nextElementSibling.querySelector("code");
const tempTextarea = document.createElement("textarea");
tempTextarea.value = codeBlock.innerText;
document.body.appendChild(tempTextarea);
tempTextarea.select();
document.execCommand("copy");
document.body.removeChild(tempTextarea);
button.textContent = "Copied!";
setTimeout(() => {
button.textContent = "Copy";
}, 2000);
}
function sleep(ms) {
if (ms > 0) return new Promise((resolve) => setTimeout(resolve, ms));
else return;
}
fastBtn.addEventListener("click", () => {
fastBtnFs += 1;
fastBtn.style.fontSize = fastBtnFs + "px";
if (ms <= 5) {
fastBtn.style.fontSize = "16px";
}
ms = ms > 5 ? Math.abs(ms - 199) : (ms = 5);
ms <= 5 ? (fastBtn.style.display = "none") : 1;
});
skipBtn.addEventListener("click", () => {
ms = 0;
displayBtn("none");
});
function displayBtn(dis) {
fastBtn.style.display = dis;
skipBtn.style.display = dis;
fastBtn.style.fontSize = "1rem";
fastBtnFs = 16;
}
inputBox.addEventListener("keyup", (event) => {
if (event.key === "Enter" && sortBtn.textContent === "Sort") sortBtn.click();
});
//Nav toggle — side drawer
let navOverlay = null;
function getOrCreateOverlay() {
if (!navOverlay) {
navOverlay = document.createElement("div");
navOverlay.className = "nav-overlay";
document.body.appendChild(navOverlay);
navOverlay.addEventListener("click", closeDrawer);
}
return navOverlay;
}
function openDrawer() {
const navLinks = document.querySelector(".nav-links");
const menuToggle = document.querySelector(".menu-toggle");
const overlay = getOrCreateOverlay();
navLinks.classList.add("active");
menuToggle.classList.add("active");
overlay.classList.add("active");
document.body.style.overflow = "hidden"; // prevent background scroll
}
function closeDrawer() {
const navLinks = document.querySelector(".nav-links");
const menuToggle = document.querySelector(".menu-toggle");
const overlay = getOrCreateOverlay();
navLinks.classList.remove("active");
menuToggle.classList.remove("active");
overlay.classList.remove("active");
document.body.style.overflow = "";
}
function toggleMenu() {
const navLinks = document.querySelector(".nav-links");
if (navLinks.classList.contains("active")) {
closeDrawer();
} else {
openDrawer();
}
}
// Close drawer when a nav link is tapped (navigating away)
document.querySelector(".nav-links").addEventListener("click", (e) => {
if (e.target.tagName === "A" && window.innerWidth <= 768) {
closeDrawer();
}
});
// Close on Escape key
document.addEventListener("keydown", (e) => {
if (e.key === "Escape") closeDrawer();
});
function randomGenerate() {
if (sortBtn.textContent == "Sorting...") return;
const random = Math.floor(Math.random() * 10) + 5;
let randomNums = [];
for (let i = 0; i < random; i++) {
let rand = Math.floor(Math.random() * 100);
randomNums.push(rand);
}
inputBox.value = randomNums.join(",");
}