forked from swapnilsparsh/30DaysOfJavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
153 lines (130 loc) · 3.76 KB
/
script.js
File metadata and controls
153 lines (130 loc) · 3.76 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
// Preloader
var preloader = document.getElementById("loading");
function loading() {
setTimeout(myfunction, 2000);
}
function myfunction() {
preloader.style.display = "none";
}
$(document).ready(function () {
renderProjects();
$(window).scroll(function () {
if ($(this).scrollTop() >= 100) {
$(".scrollToTop").fadeIn();
} else {
$(".scrollToTop").fadeOut();
}
});
$(".scrollToTop").click(function (e) {
e.preventDefault();
$("html, body").animate({ scrollTop: 0 }, 500);
return false;
});
});
function checkTheme() {
console.log("Checking theme");
const body = document.querySelector("body");
const button = document.getElementById("toggle");
const themeLocalStorage = localStorage.getItem("theme");
console.log("Valor em themeLocalStorage:", themeLocalStorage);
if (themeLocalStorage) {
console.log(`diferente de null`);
body.className = themeLocalStorage;
}
if (themeLocalStorage == "light-mode") {
button.innerHTML = "☀️";
} else {
button.innerHTML = "🌙";
}
}
document.addEventListener("DOMContentLoaded", checkTheme);
function toggleDarkLight() {
const body = document.querySelector("body");
const button = document.getElementById("toggle");
console.log(body.classList);
if (body.classList.contains("dark-mode")) {
body.className = "light-mode";
button.innerHTML = "☀️";
localStorage.setItem("theme", "light-mode");
} else {
body.className = "dark-mode";
button.innerHTML = "🌙";
localStorage.setItem("theme", "dark-mode");
}
}
let currentPage = 1;
var x = document.getElementsByClassName("item");
function search_project() {
let input = document.getElementById("searchbar").value.toLowerCase();
let projectsContainer = document.getElementById("projectsContainer");
let projects = document.getElementsByClassName("item");
let found = false;
for (let i = 0; i < projects.length; i++) {
const projectName = projects[i].querySelector("h4").innerHTML.toLowerCase();
if (!projectName.includes(input)) {
projects[i].style.display = "none";
} else {
projects[i].style.display = "list-item";
found = true;
}
}
let existingMessage = document.getElementById("noProjectMessage");
if (existingMessage) {
existingMessage.remove();
}
if (!found) {
let noProjectMessage = document.createElement("h1");
noProjectMessage.id = "noProjectMessage";
noProjectMessage.innerHTML = "No project found";
projectsContainer.appendChild(noProjectMessage);
}
}
function renderProjects() {
window.scrollTo({ top: 0, behavior: "smooth" });
const projectsPerPage = 30;
const startIndex = (currentPage - 1) * projectsPerPage;
const endIndex = startIndex + projectsPerPage;
for (let i = 0; i < x.length; i++) {
if (i >= startIndex && i < endIndex) {
x[i].style.display = "list-item";
} else {
x[i].style.display = "none";
}
}
}
function goToPage(page) {
currentPage = page;
renderProjects();
const buttons = document.querySelectorAll(".pagination button");
buttons.forEach((button) => {
if (button.textContent == currentPage) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
}
function goToNextPage() {
if (currentPage < Math.ceil(x.length / 30)) {
currentPage++;
renderProjects();
updateActiveButton();
}
}
function goToPreviousPage() {
if (currentPage > 1) {
currentPage--;
renderProjects();
updateActiveButton();
}
}
function updateActiveButton() {
const buttons = document.querySelectorAll(".pagination button");
buttons.forEach((button) => {
if (parseInt(button.textContent) === currentPage) {
button.classList.add("active");
} else {
button.classList.remove("active");
}
});
}