personal-website/script.js

75 lines
2.3 KiB
JavaScript
Raw Normal View History

2024-02-18 04:41:05 -06:00
const rightSideContainer = document.getElementById('right-side');
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);
2024-02-18 04:41:05 -06:00
await updateOpacity(rightSideContainer, true);
2023-06-08 11:14:18 -05:00
}
// 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
}
swapButton.addEventListener('click', swapContent);