-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
49 lines (44 loc) · 1.51 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
//Variáveis iniciais
const main = document.querySelector("main");
const root = document.querySelector(":root");
const input = document.getElementById("input");
//Controle dos botões da calculadora
document.querySelectorAll(".charKey").forEach(function (key) {
key.addEventListener("click", function () {
const value = key.dataset.value;
input.value += value;
});
});
document.getElementById("clear").addEventListener("click", function () {
input.value = "";
input.classList.remove("error");
input.focus();
});
document.getElementById("equal").addEventListener("click", calculate);
//Calcular o resultado da operação
function calculate() {
try {
const result = eval(input.value);
input.value = result;
input.classList.remove("error");
} catch (error) {
input.classList.add("error");
input.animate(animation, animationTiming);
}
}
//Mudar o tema do site
document.getElementById("themeSwitcher").addEventListener("click", function () {
if (main.dataset.theme === "dark") {
root.style.setProperty("--bg-color", "#f1f5f9");
root.style.setProperty("--border-color", "#aaa");
root.style.setProperty("--font-color", "#212529");
root.style.setProperty("--primary-color", "#26834a");
main.dataset.theme = "light";
} else {
root.style.setProperty("--bg-color", "#212529");
root.style.setProperty("--border-color", "#666");
root.style.setProperty("--font-color", "#f1f5f9");
root.style.setProperty("--primary-color", "#4dff91");
main.dataset.theme = "dark";
}
});