personal-website/script.js

125 lines
3.6 KiB
JavaScript
Raw Normal View History

2023-06-08 11:14:18 -05:00
const contentContainer = document.getElementById('content');
2024-02-18 04:41:05 -06:00
const swapButton = document.getElementById('swap');
2023-09-15 13:16:54 -05:00
const ageElement = document.getElementById('age');
2023-06-08 11:14:18 -05:00
const points = document.getElementById('points');
const box = document.getElementById('box');
const boxPoints = document.querySelector('#points span');
let bonusPoints = Number(localStorage.getItem('points') ?? 0);
let currentTheme = localStorage.getItem('theme') ?? 'light';
const wait = (ms) => new Promise(resolve => setTimeout(resolve, ms * 1000))
async function updateOpacity(block, show) {
if (!show) {
block.style.opacity = '0';
await wait(0.3);
block.style.display = 'none';
} else {
2024-02-18 04:41:05 -06:00
block.style.display = 'flex';
2023-06-08 11:14:18 -05:00
await wait(0.3);
block.style.opacity = '1';
}
2024-02-22 02:54:05 -06:00
2023-06-08 11:14:18 -05:00
block.classList.toggle('visible');
}
function toggleTheme() {
document.body.classList.toggle('dark');
currentTheme = currentTheme === 'light' ? 'dark' : 'light';
localStorage.setItem('theme', currentTheme);
}
function resetPoints() {
localStorage.removeItem('points');
bonusPoints = 0;
boxPoints.textContent = bonusPoints;
}
2024-02-18 04:41:05 -06:00
function swapContent() {
2024-02-22 02:54:05 -06:00
if (window.innerWidth < 768) {
contentContainer.style.flexDirection =
contentContainer.style.flexDirection === 'column-reverse' ? 'column' : 'column-reverse';
} else {
contentContainer.style.flexDirection =
contentContainer.style.flexDirection === 'row-reverse' ? 'row' : 'row-reverse';
}
2024-02-18 04:41:05 -06:00
}
2023-06-08 11:14:18 -05:00
// listeners
if (box) box.addEventListener('click', () => {
bonusPoints++;
localStorage.setItem('points', bonusPoints);
boxPoints.textContent = bonusPoints;
})
window.onload = async () => {
if (boxPoints) boxPoints.textContent = bonusPoints;
if (box) await updateOpacity(box, true);
}
// update theme
if (currentTheme === 'dark')
2023-09-15 13:16:54 -05:00
document.body.classList.add('dark');
// update age
2024-02-18 04:41:05 -06:00
if (ageElement) {
2023-09-15 13:16:54 -05:00
const difference = (Date.now() - 1182722400000) / (1000 * 60 * 60 * 24 * 365);
ageElement.innerText = difference.toFixed(4);
2024-02-18 04:41:05 -06:00
}
2024-02-28 13:07:22 -06:00
swapButton.addEventListener('click', swapContent);
const accordions = document.querySelectorAll('.accordion');
function closeOtherAccordions(accordion) {
accordions.forEach(acc => {
2024-03-07 03:28:23 -06:00
if (acc !== accordion) {
const panel = accordion.children[1];
2024-02-28 13:07:22 -06:00
acc.classList.remove('active');
2024-03-07 03:28:23 -06:00
panel.style.maxHeight = null;
}
2024-02-28 13:07:22 -06:00
});
}
function openAccordion(accordion) {
closeOtherAccordions(accordion);
2024-03-07 03:28:23 -06:00
const panel = accordion.children[1];
2024-02-28 13:07:22 -06:00
accordion.classList.toggle('active');
2024-03-07 03:28:23 -06:00
console.log(panel, panel.style.maxHeight)
if (panel.style.maxHeight)
panel.style.maxHeight = null
else
panel.style.maxHeight = panel.scrollHeight + "px"
2024-02-28 13:07:22 -06:00
}
accordions.forEach(accordion => {
accordion.addEventListener('click', () => openAccordion(accordion));
});
// Hide or show columns
const columns = document.querySelectorAll('#content > div');
function showColumn(event, column) {
// Check if the click was on actual column, not on its children
if (event.target !== column) return;
columns.forEach(col => {
if (col === column) return;
col.dataset.closed = 'true';
});
column.dataset.closed = column.dataset.closed === 'true' ? 'false' : 'true';
}
// Apply only to desktop
if (window.innerWidth > 768) {
columns.forEach(column => {
column.addEventListener('click', (event) => showColumn(event, column));
});
}