-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
75 lines (67 loc) · 2.45 KB
/
script.js
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
const themeToggle = document.getElementById('theme-toggle');
const body = document.body;
const demoImage = document.getElementById('demo-image');
function setCookie(name, value, days) {
let expires = "";
if (days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/";
}
function getCookie(name) {
const nameEQ = name + "=";
const ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
function setTheme(isDark) {
if (isDark) {
body.classList.add('dark-mode');
themeToggle.classList.replace('bi-sun-fill', 'bi-moon-fill');
demoImage.src = 'assets/homepage-darkmode.png';
} else {
body.classList.remove('dark-mode');
themeToggle.classList.replace('bi-moon-fill', 'bi-sun-fill');
demoImage.src = 'assets/homepage-lightmode.png';
}
}
// Default to dark mode if there's no saved theme
const savedTheme = getCookie('theme');
setTheme(savedTheme !== 'light');
themeToggle.addEventListener('click', () => {
const isDarkMode = body.classList.toggle('dark-mode');
setTheme(isDarkMode);
setCookie('theme', isDarkMode ? 'dark' : 'light', 30);
});
document.addEventListener('DOMContentLoaded', function () {
const element = document.querySelector('.text-switcher');
const words = ["website", "app", "API", "service", "database", "server", "platform", "network"];
let index = 0;
let charIndex = 0;
const typingSpeed = 150;
let isDeleting = false;
function typeWriter() {
if (charIndex < words[index].length && !isDeleting) {
element.textContent += words[index].charAt(charIndex);
charIndex++;
setTimeout(typeWriter, typingSpeed);
} else if (charIndex > 0 && isDeleting) {
element.textContent = element.textContent.slice(0, -1);
charIndex--;
setTimeout(typeWriter, typingSpeed / 2);
} else {
isDeleting = !isDeleting;
if (!isDeleting) {
index = (index + 1) % words.length;
}
setTimeout(typeWriter, 500);
}
}
typeWriter();
});