-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (53 loc) · 2.26 KB
/
script.js
File metadata and controls
61 lines (53 loc) · 2.26 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
document.querySelectorAll('.unit-btn').forEach(btn => {
btn.addEventListener('click', function() {
const unit = this.dataset.unit;
const dayElement = this.closest('.day');
dayElement.querySelectorAll('.unit-btn').forEach(b => b.classList.remove('active'));
this.classList.add('active');
dayElement.querySelectorAll('.temp, .temp-value').forEach(temp => {
let tempText = temp.textContent;
let temperatures = tempText.match(/\d+/g);
if (temperatures) {
temperatures = temperatures.map(t => parseInt(t));
if (unit === 'F') {
temperatures = temperatures.map(t => Math.round(t * 9/5 + 32));
} else {
temperatures = temperatures.map(t => Math.round((t - 32) * 5/9));
}
if (temp.classList.contains('temp')) {
temp.textContent = `${temperatures[0]}°${unit}/${temperatures[1]}°${unit}`;
} else {
const hourIndex = [...temp.parentNode.parentNode.children].indexOf(temp.parentNode);
temp.textContent = temperatures[hourIndex];
temp.nextElementSibling.textContent = `°${unit}`;
}
}
});
});
});
document.querySelectorAll('.favorite-btn').forEach(btn => {
btn.addEventListener('click', function() {
this.textContent = this.textContent === '☆' ? '★' : '☆';
this.classList.toggle('active');
});
});
document.querySelectorAll('.toggle-button').forEach(button => {
button.addEventListener('click', (e) => {
e.stopPropagation();
const day = button.closest('.day');
day.classList.toggle('expanded');
});
});
function highlightCurrentHour() {
const currentHour = new Date().getHours();
document.querySelectorAll('.hourly-temp tr').forEach(row => {
const timeCell = row.querySelector('.time-cell span');
const hour = parseInt(timeCell.textContent);
if (hour === currentHour) {
row.classList.add('current-hour');
}
});
}
document.addEventListener('DOMContentLoaded', function() {
highlightCurrentHour();
});