Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (63 loc) · 2.48 KB
/
Copy pathscript.js
File metadata and controls
72 lines (63 loc) · 2.48 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
const hoursEl = document.querySelector(".hoursNumber");
const minutesEl = document.querySelector(".minutesNumber");
const secondsEl = document.querySelector(".secondsNumber");
const dateEl = document.querySelector("#dateDisplay");
const timezoneEl = document.querySelector("#timezoneDisplay");
const timezoneSelect = document.querySelector("#timezoneSelect");
const localTimezone = Intl.DateTimeFormat().resolvedOptions().timeZone || "UTC";
const supportedTimezones = typeof Intl.supportedValuesOf === "function"
? Intl.supportedValuesOf("timeZone")
: [];
const timezones = [...new Set([localTimezone, "UTC", ...supportedTimezones])];
for (const timezone of timezones) {
const option = document.createElement("option");
option.value = timezone;
option.textContent = timezone.replaceAll("_", " ");
timezoneSelect.appendChild(option);
}
timezoneSelect.value = localTimezone;
function updateClock() {
const timezone = timezoneSelect.value;
const now = new Date();
const parts = Object.fromEntries(
new Intl.DateTimeFormat("en-GB", {
timeZone: timezone,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
hourCycle: "h23",
})
.formatToParts(now)
.map(({ type, value }) => [type, value]),
);
hoursEl.textContent = parts.hour;
minutesEl.textContent = parts.minute;
secondsEl.textContent = parts.second;
dateEl.textContent = new Intl.DateTimeFormat(undefined, {
timeZone: timezone,
weekday: "long",
month: "long",
day: "numeric",
year: "numeric",
}).format(now);
timezoneEl.textContent = timezone;
}
timezoneSelect.addEventListener("change", updateClock);
updateClock();
window.setInterval(updateClock, 1000);
const yearEl = document.querySelector("#currentYear");
const topBtn = document.querySelector("#topBtn");
if (yearEl) yearEl.textContent = new Date().getFullYear();
const updateTopButton = () => {
if (!topBtn) return;
const isVisible = window.scrollY > 260;
topBtn.classList.toggle("show", isVisible);
topBtn.tabIndex = isVisible ? 0 : -1;
topBtn.setAttribute("aria-hidden", String(!isVisible));
};
window.addEventListener("scroll", updateTopButton, { passive: true });
updateTopButton();
topBtn?.addEventListener("click", () => {
const reduceMotion = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
window.scrollTo({ top: 0, behavior: reduceMotion ? "auto" : "smooth" });
});