adventskalender/shared/js/position.js aktualisiert
This commit is contained in:
@@ -1,11 +1,11 @@
|
|||||||
/**
|
/**
|
||||||
* Bratonien Adventskalender – Positionierung aller Türchen und Flügel (2025)
|
* Bratonien Adventskalender – Türchen-Positionierung (2025)
|
||||||
* ---------------------------------------------------------
|
* ---------------------------------------------------------
|
||||||
* Liest data-top / data-left / data-width aus allen relevanten Elementen
|
* Liest data-top / data-left / data-width aus jedem Element
|
||||||
* und positioniert sie exakt auf dem sichtbaren Hintergrundbild.
|
* und positioniert .door, .fluegel, .openfield korrekt über dem Bild.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function positionAllElements() {
|
function positionAllDoors() {
|
||||||
const cont = document.querySelector('.kalenderbild');
|
const cont = document.querySelector('.kalenderbild');
|
||||||
const img = cont?.querySelector('img');
|
const img = cont?.querySelector('img');
|
||||||
if (!cont || !img) return;
|
if (!cont || !img) return;
|
||||||
@@ -13,67 +13,46 @@ function positionAllElements() {
|
|||||||
const imgRect = img.getBoundingClientRect();
|
const imgRect = img.getBoundingClientRect();
|
||||||
const contRect = cont.getBoundingClientRect();
|
const contRect = cont.getBoundingClientRect();
|
||||||
|
|
||||||
// === Einzeltüren & Openfields ===
|
// Normale Türen + Openfields
|
||||||
document.querySelectorAll('.door').forEach(door => {
|
document.querySelectorAll('.door, .openfield').forEach(el => {
|
||||||
const day = door.dataset.day;
|
const topPct = parseFloat(el.dataset.top) || 0;
|
||||||
const topPct = parseFloat(door.dataset.top) || 0;
|
const leftPct = parseFloat(el.dataset.left) || 0;
|
||||||
const leftPct = parseFloat(door.dataset.left) || 0;
|
const widthPct = parseFloat(el.dataset.width) || 10;
|
||||||
const widthPct = parseFloat(door.dataset.width) || 10;
|
|
||||||
|
|
||||||
const w = imgRect.width * (widthPct / 100);
|
const widthPx = imgRect.width * (widthPct / 100);
|
||||||
const t = imgRect.top - contRect.top + imgRect.height * (topPct / 100);
|
const heightPx = widthPx; // quadratisch
|
||||||
const l = imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2;
|
|
||||||
|
|
||||||
door.style.position = 'absolute';
|
el.style.position = 'absolute';
|
||||||
door.style.width = w + 'px';
|
el.style.width = widthPx + 'px';
|
||||||
door.style.height = w + 'px';
|
el.style.height = heightPx + 'px';
|
||||||
door.style.top = t + 'px';
|
el.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
|
||||||
door.style.left = l + 'px';
|
el.style.left = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + '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) ===
|
// Flügel: jeweils halbe Breite, doppelte Höhe (nicht quadratisch!)
|
||||||
document.querySelectorAll('.fluegel').forEach(flugel => {
|
document.querySelectorAll('.fluegel').forEach(fluegel => {
|
||||||
const topPct = parseFloat(flugel.dataset.top) || 0;
|
const topPct = parseFloat(fluegel.dataset.top) || 0;
|
||||||
const leftPct = parseFloat(flugel.dataset.left) || 0;
|
const leftPct = parseFloat(fluegel.dataset.left) || 0;
|
||||||
const widthPct = parseFloat(flugel.dataset.width) || 5;
|
const widthPct = parseFloat(fluegel.dataset.width) || 10;
|
||||||
|
|
||||||
const fullW = imgRect.width * (widthPct / 100);
|
const widthPx = imgRect.width * (widthPct / 100) / 2;
|
||||||
const w = fullW / 2;
|
const heightPx = widthPx * 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';
|
fluegel.style.position = 'absolute';
|
||||||
flugel.style.width = w + 'px';
|
fluegel.style.width = widthPx + 'px';
|
||||||
flugel.style.height = h + 'px';
|
fluegel.style.height = heightPx + 'px';
|
||||||
flugel.style.top = t + 'px';
|
fluegel.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
|
||||||
|
fluegel.style.left = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + '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('load', positionAllDoors);
|
||||||
window.addEventListener('resize', positionAllElements);
|
window.addEventListener('resize', positionAllDoors);
|
||||||
|
|
||||||
window.addEventListener('orientationchange', () => {
|
window.addEventListener('orientationchange', () => {
|
||||||
setTimeout(positionAllElements, 300);
|
setTimeout(positionAllDoors, 300);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.addEventListener('visibilitychange', () => {
|
document.addEventListener('visibilitychange', () => {
|
||||||
if (document.visibilityState === 'visible') {
|
if (document.visibilityState === 'visible') {
|
||||||
setTimeout(positionAllElements, 200);
|
setTimeout(positionAllDoors, 200);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Reference in New Issue
Block a user