-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
202 lines (167 loc) · 5.84 KB
/
script.js
File metadata and controls
202 lines (167 loc) · 5.84 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
// Select the nav items and the indicator span
const navItems = document.querySelectorAll("nav ul li");
const indicator = document.querySelector("nav span");
// Function to set the indicator position
function setIndicatorPosition(item) {
const itemLeft =
item.offsetLeft + item.offsetWidth / 2 - indicator.offsetWidth / 2;
indicator.style.left = `${itemLeft}px`;
}
// Add click event listeners to each nav item
navItems.forEach((item, index) => {
item.addEventListener("click", () => {
// Save the index of the clicked item to localStorage
localStorage.setItem("activeNavIndex", index);
// Move the indicator and update the active class
setIndicatorPosition(item);
navItems.forEach((li) => li.classList.remove("active"));
item.classList.add("active");
});
});
// Initialize the indicator position on page load
function initializeNav() {
const savedIndex = localStorage.getItem("activeNavIndex");
let initialIndex = savedIndex !== null ? parseInt(savedIndex) : 0; // Default to Home
// Ensure the Home tab (index 0) is selected on first visit
if (
window.location.pathname === "/" ||
window.location.pathname.includes("index.html")
) {
initialIndex = 0; // Set Home as default
}
const initialItem = navItems[initialIndex];
// Set the indicator and active class based on saved state
setIndicatorPosition(initialItem);
navItems.forEach((li) => li.classList.remove("active"));
initialItem.classList.add("active");
}
// Run the initialization function on page load
initializeNav();
// nav scroll
document.addEventListener("scroll", () => {
const nav = document.querySelector("nav");
if (window.scrollY > 400) {
nav.classList.add("scrolled"); // Add class when scrolled more than 100px
} else {
nav.classList.remove("scrolled"); // Remove class when back to the top
}
});
// svg rotation on scroll
window.addEventListener("scroll", function () {
const svg = document.querySelector(".coolness svg");
// Get the scroll position as a percentage of the page height
const scrollY = window.scrollY;
const scrollHeight =
document.documentElement.scrollHeight - window.innerHeight;
// Calculate the rotation degree based on scroll
const rotationDegree = (scrollY / scrollHeight) * 720; // Max rotation is 360 degrees
svg.style.transform = `rotate(${rotationDegree}deg)`; // Apply the rotation
});
// svg
// Get the SVG path element
const path = document.querySelector(".cls-1");
// Function to check if element is in viewport
function isInViewport(element) {
const rect = element.getBoundingClientRect();
return rect.top <= window.innerHeight && rect.bottom >= 0;
}
// Event listener for scrolling
window.addEventListener("scroll", () => {
if (isInViewport(path)) {
// Trigger animation when the SVG is in the viewport
path.style.strokeDashoffset = "0";
}
});
// profile page
const texts = [
" PAUL OBIERO",
"A WEB DEVELOPER",
"A STUDENT",
"A TECH ENTHUSIAST"
];
let currentIndex = 0;
function changeText() {
const dynamicText = document.getElementById("dynamicText");
currentIndex = (currentIndex + 1) % texts.length; // Cycle through array
dynamicText.textContent = texts[currentIndex];
}
setInterval(changeText, 2000);
// dark mode
const toggleButton = document.getElementById("toggle-theme");
const body = document.body;
const sunIcon = document.getElementById("sun-icon");
const moonIcon = document.getElementById("moon-icon");
// Check if the user prefers dark mode from localStorage
if (localStorage.getItem("theme") === "dark") {
body.classList.add("dark-mode");
moonIcon.style.display = "block";
sunIcon.style.display = "none";
}
// Toggle the theme and icons
toggleButton.addEventListener("click", () => {
body.classList.toggle("dark-mode");
if (body.classList.contains("dark-mode")) {
moonIcon.style.display = "block";
sunIcon.style.display = "none";
localStorage.setItem("theme", "dark");
} else {
moonIcon.style.display = "none";
sunIcon.style.display = "block";
localStorage.setItem("theme", "light");
}
});
// download
// document.getElementById("downloadBtn").addEventListener("click", function () {
// const link = document.createElement("a");
// link.href = "./Paul Obiero - Full Stack Web Developer - Resume.pdf";
// link.download = "My_CV.pdf";
// document.body.appendChild(link);
// link.click();
// document.body.removeChild(link);
// });
// movement of the cursor
window.addEventListener("mousemove", (e) => {
let cursor = document.getElementById("cursor");
cursor.style.top = "${e.clientY}px";
cursor.style.left = "${e.clientX}px";
});
window.addEventListener("mousemove", (e) => {
let cursor = document.getElementById("cursor");
if (!cursor) return; // Ensure the element exists
setTimeout(() => {
cursor.style.top = e.clientY + "px";
cursor.style.left = e.clientX + "px";
}, 60);
});
// cursor transition
const cursor = document.querySelector(".cursor");
const hoverElements = document.querySelectorAll(
".grid-item a, nav ul li a ,.icons i,.hero-img .img ,footer li a, .grid-item img,.custom-video "
); // Added video elements
hoverElements.forEach((element) => {
element.addEventListener("mouseenter", () => {
cursor.style.transform = "scale(7.5)"; // Increase size
});
element.addEventListener("mouseleave", () => {
cursor.style.transform = "scale(1)"; // Reset size
});
});
// cursor transition
// lingo
const icons = document.querySelectorAll(".reveal-icon");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry, index) => {
if (entry.isIntersecting) {
setTimeout(() => {
entry.target.classList.add("show");
}, index * 150); // delay each icon
observer.unobserve(entry.target); // remove if you only want it to trigger once
}
});
},
{
threshold: 0.2
}
);
icons.forEach((icon) => observer.observe(icon));