forked from VAIBHAVBABELE/vaibhavbabele.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
170 lines (142 loc) · 5.3 KB
/
index.js
File metadata and controls
170 lines (142 loc) · 5.3 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
// ===== Navbar & Scroll Handling =====
const element = document.getElementById('scroll-hide');
const mode = document.getElementById("mode");
const load = document.getElementById("onload");
function checkWindowSize() {
if (!element) return;
if (window.innerWidth < 1000) {
window.addEventListener('scroll', function () {
if (window.scrollY > 10) {
element.style.display = 'none';
}
});
} else {
element.style.display = 'flex';
}
}
window.addEventListener('load', checkWindowSize);
window.addEventListener('resize', checkWindowSize);
/* ===== Mobile Menu Toggle ===== */
function initNavbarEvents() {
const clickBtn = document.getElementById('click');
const navLinks = document.querySelectorAll("nav ul li a"); // menu links
if (clickBtn) {
clickBtn.addEventListener('click', function () {
if (element) element.style.display = 'block';
});
}
if (!clickBtn) return;
// Close nav when a link is clicked
navLinks.forEach(link => {
link.addEventListener("click", () => {
clickBtn.checked = false; // uncheck hamburger = menu closes
});
});
}
// Trigger when navbar dynamically loaded
document.addEventListener("navbarLoaded", initNavbarEvents);
document.addEventListener("DOMContentLoaded", initNavbarEvents);
// ===== Dark Mode Toggle =====
window.onload = function () {
const isDark = localStorage.getItem('mode') === 'true';
document.body.classList.toggle('dark-mode', isDark);
updateModeIcon();
}
function updateModeIcon() {
if (!mode) return;
const path = window.location.pathname;
const isDeepPage = ['/summary', '/assistant', '/pr-contribution', '/certificate'].some(subpath =>
path.includes('/pages' + subpath)
);
let imgPathPrefix = '';
if (isDeepPage) {
imgPathPrefix = '../../images/';
} else if (path.includes('/pages') || path.includes('/games')) {
imgPathPrefix = '../images/';
} else {
imgPathPrefix = 'images/';
}
mode.src = document.body.classList.contains("dark-mode") ? imgPathPrefix + "sun.png" : imgPathPrefix + "moon.png";
}
if (mode) {
mode.onclick = function () {
const wasDarkmode = localStorage.getItem('mode') === 'true';
const newMode = !wasDarkmode;
localStorage.setItem('mode', newMode);
document.body.classList.toggle("dark-mode", newMode);
updateModeIcon();
}
}
// ===== Copyright Year =====
document.addEventListener("DOMContentLoaded", function () {
const year = new Date().getFullYear();
const copyright = document.getElementById("copyright");
if (copyright) {
copyright.textContent = `© ${year} nitramitra | All Rights Reserved`;
}
});
// ===== Google Translate Toggle =====
function googleTranslateElementInit() {
new google.translate.TranslateElement({
pageLanguage: 'en',
includedLanguages: 'en,hi',
layout: google.translate.TranslateElement.InlineLayout.SIMPLE
}, 'google_translate_element');
}
// Dynamically load Google Translate script once
if (!document.querySelector('script[src*="translate.google.com"]')) {
const translateScript = document.createElement('script');
translateScript.src = "//translate.google.com/translate_a/element.js?cb=googleTranslateElementInit";
document.body.appendChild(translateScript);
}
function waitForTranslate(callback) {
const interval = setInterval(() => {
const select = document.querySelector(".goog-te-combo");
if (select) {
clearInterval(interval);
callback(select);
}
}, 500);
}
function initLanguageToggle() {
const langToggle = document.getElementById("lang-toggle");
if (!langToggle) return;
waitForTranslate((select) => {
// Apply saved language immediately
const savedLang = localStorage.getItem("lang") || "en";
select.value = savedLang;
select.dispatchEvent(new Event("change"));
langToggle.innerText = savedLang === "en" ? "हिंदी" : "English";
// Toggle click
langToggle.addEventListener("click", function () {
const newLang = select.value === "en" ? "hi" : "en";
select.value = newLang;
select.dispatchEvent(new Event("change"));
langToggle.innerText = newLang === "en" ? "हिंदी" : "English";
localStorage.setItem("lang", newLang);
});
});
}
// Run on every page load
document.addEventListener("DOMContentLoaded", initLanguageToggle);
document.addEventListener("navbarLoaded", initLanguageToggle);
document.addEventListener("DOMContentLoaded", () => {
const sections = document.querySelectorAll("section[id]");
const navLinks = document.querySelectorAll("nav ul li a");
window.addEventListener("scroll", () => {
let current = "";
sections.forEach(section => {
const sectionTop = section.offsetTop - 100; // adjust offset for header
const sectionHeight = section.clientHeight;
if (scrollY >= sectionTop && scrollY < sectionTop + sectionHeight) {
current = section.getAttribute("id");
}
});
navLinks.forEach(link => {
link.classList.remove("active");
if (link.getAttribute("href") === `#${current}`) {
link.classList.add("active");
}
});
});
});