Files
Bratonien-Adventskalender/adventskalender/shared/js/position.js

79 lines
2.8 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* Bratonien Adventskalender Positionierung aller Türchen und Flügel (2025)
* ---------------------------------------------------------
* Liest data-top / data-left / data-width aus allen relevanten Elementen
* und positioniert sie exakt auf dem sichtbaren Hintergrundbild.
*/
function positionAllElements() {
const cont = document.querySelector('.kalenderbild');
const img = cont?.querySelector('img');
if (!cont || !img) return;
const imgRect = img.getBoundingClientRect();
const contRect = cont.getBoundingClientRect();
// === Einzeltüren & Openfields ===
document.querySelectorAll('.door').forEach(door => {
const day = door.dataset.day;
const topPct = parseFloat(door.dataset.top) || 0;
const leftPct = parseFloat(door.dataset.left) || 0;
const widthPct = parseFloat(door.dataset.width) || 10;
const w = imgRect.width * (widthPct / 100);
const t = imgRect.top - contRect.top + imgRect.height * (topPct / 100);
const l = imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2;
door.style.position = 'absolute';
door.style.width = w + 'px';
door.style.height = w + 'px';
door.style.top = t + 'px';
door.style.left = l + 'px';
const field = document.querySelector(`.openfield[data-day="${day}"]`);
if (field) {
field.style.position = 'absolute';
field.style.width = w + 'px';
field.style.height = w + 'px';
field.style.top = t + 'px';
field.style.left = l + 'px';
}
});
// === Flügel (linke und rechte Seite) ===
document.querySelectorAll('.fluegel').forEach(flugel => {
const topPct = parseFloat(flugel.dataset.top) || 0;
const leftPct = parseFloat(flugel.dataset.left) || 0;
const widthPct = parseFloat(flugel.dataset.width) || 5;
const fullW = imgRect.width * (widthPct / 100);
const w = fullW / 2;
const h = fullW; // volle Türhöhe = Breite der gesamten Tür
const t = imgRect.top - contRect.top + imgRect.height * (topPct / 100);
const l = imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2;
flugel.style.position = 'absolute';
flugel.style.width = w + 'px';
flugel.style.height = h + 'px';
flugel.style.top = t + 'px';
if (flugel.classList.contains('left')) {
flugel.style.left = l + 'px';
} else if (flugel.classList.contains('right')) {
flugel.style.left = (l + w) + 'px';
}
});
}
window.addEventListener('load', positionAllElements);
window.addEventListener('resize', positionAllElements);
window.addEventListener('orientationchange', () => {
setTimeout(positionAllElements, 300);
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
setTimeout(positionAllElements, 200);
}
});