adventskalender/shared/js/position.js aktualisiert

This commit is contained in:
2025-11-04 18:32:03 +00:00
parent 5767d457cc
commit 56b169cf10

View File

@@ -1,3 +1,10 @@
/**
* Bratonien Adventskalender Türchen-Positionierung (2025)
* ---------------------------------------------------------
* Liest data-top / data-left / data-width aus jedem .fluegel und .openfield
* und positioniert die Elemente exakt auf dem sichtbaren Bild.
*/
function positionAllDoors() { function positionAllDoors() {
const cont = document.querySelector('.kalenderbild'); const cont = document.querySelector('.kalenderbild');
const img = cont?.querySelector('img'); const img = cont?.querySelector('img');
@@ -6,42 +13,43 @@ function positionAllDoors() {
const imgRect = img.getBoundingClientRect(); const imgRect = img.getBoundingClientRect();
const contRect = cont.getBoundingClientRect(); const contRect = cont.getBoundingClientRect();
// Türchen, Flügel und Felder positionieren // Flügel (bei Doppeltüren) positionieren
document.querySelectorAll('[data-day]').forEach(el => { document.querySelectorAll('.fluegel').forEach(flg => {
const day = el.dataset.day; const day = flg.dataset.day;
const topPct = parseFloat(el.dataset.top) || 0; const topPct = parseFloat(flg.dataset.top) || 0;
const leftPct = parseFloat(el.dataset.left) || 0; const leftPct = parseFloat(flg.dataset.left) || 0;
const widthPct = parseFloat(el.dataset.width) || 10; const widthPct = parseFloat(flg.dataset.width) || 10;
const fullWidth = imgRect.width * (widthPct / 100); const w = imgRect.width * (widthPct / 100);
const fullTop = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px'; const h = w * 2; // Flügel doppelt so hoch wie breit
const fullLeft = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + 'px';
el.style.position = 'absolute'; flg.style.position = 'absolute';
el.style.top = fullTop; flg.style.width = w + 'px';
el.style.left = fullLeft; flg.style.height = h + 'px';
flg.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
flg.style.left = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + 'px';
});
if (el.classList.contains('hinge-left') || el.classList.contains('hinge-right')) { // Openfields korrekt positionieren
el.style.width = fullWidth + 'px'; document.querySelectorAll('.openfield').forEach(field => {
el.style.height = fullWidth + 'px'; const day = field.dataset.day;
const topPct = parseFloat(field.dataset.top) || 0;
const leftPct = parseFloat(field.dataset.left) || 0;
const widthPct = parseFloat(field.dataset.width) || 10;
} else if (el.classList.contains('fluegel')) { const w = imgRect.width * (widthPct / 100);
el.style.width = fullWidth + 'px';
el.style.height = (fullWidth * 2) + 'px';
} else if (el.classList.contains('openfield')) { const hasFluegel = document.querySelector(`.fluegel[data-day="${day}"]`);
el.style.width = fullWidth + 'px'; const h = hasFluegel ? w * 2 : w;
el.style.height = (fullWidth * 2) + 'px';
} else if (el.classList.contains('door-number')) { field.style.position = 'absolute';
el.style.transform = 'translate(-50%, -50%)'; field.style.width = w + 'px';
el.style.left = '50%'; field.style.height = h + 'px';
el.style.top = '50%'; field.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
} field.style.left = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + 'px';
}); });
} }
// Events
window.addEventListener('load', positionAllDoors); window.addEventListener('load', positionAllDoors);
window.addEventListener('resize', positionAllDoors); window.addEventListener('resize', positionAllDoors);
window.addEventListener('orientationchange', () => { window.addEventListener('orientationchange', () => {