-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
94 lines (77 loc) · 2.38 KB
/
script.js
File metadata and controls
94 lines (77 loc) · 2.38 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
const taskInput = document.getElementById("taskInput");
const addTaskBtn = document.getElementById("addTaskBtn");
const taskList = document.getElementById("taskList");
addTaskBtn.addEventListener("click", addTask);
taskInput.addEventListener("keydown", e => {
if (e.key === "Enter") addTask();
});
function addTask() {
const text = taskInput.value.trim();
if (!text) return alert("Please enter a task");
const li = document.createElement("li");
li.className = "task-item";
li.innerHTML = `
<div class="task-left">
<input type="checkbox" class="taskCheckbox" />
<span>${text}</span>
</div>
<div class="actions">
<button class="edit">✏️</button>
<button class="delete">🗑️</button>
</div>
`;
const checkbox = li.querySelector(".taskCheckbox");
const span = li.querySelector("span");
// --- checkbox handling ---
checkbox.addEventListener("change", () => {
span.classList.toggle("completed", checkbox.checked);
updateProgress();
});
// --- delete button ---
li.querySelector(".delete").addEventListener("click", () => {
li.remove();
updateProgress();
});
// --- edit button ---
li.querySelector(".edit").addEventListener("click", () => {
const newText = prompt("Edit task:", span.textContent);
if (newText !== null) span.textContent = newText.trim();
});
taskList.appendChild(li);
taskInput.value = "";
updateProgress();
}
// --- progress updater ---
function updateProgress() {
const boxes = document.querySelectorAll(".taskCheckbox");
const total = boxes.length;
const checked = [...boxes].filter(b => b.checked).length;
console.log(`${checked}/${total}`);
if (total && checked === total) {
launchConfetti();
}
}
// --- confetti celebration ---
function launchConfetti() {
const duration = 3 * 1000; // 3 seconds
const animationEnd = Date.now() + duration;
const defaults = { startVelocity: 30, spread: 360, ticks: 60, zIndex: 1000 };
function randomInRange(min, max) {
return Math.random() * (max - min) + min;
}
const interval = setInterval(() => {
const timeLeft = animationEnd - Date.now();
if (timeLeft <= 0) {
return clearInterval(interval);
}
const particleCount = 50 * (timeLeft / duration);
confetti({
...defaults,
particleCount,
origin: {
x: Math.random(),
y: Math.random() - 0.2
}
});
}, 250);
}