adventskalender/shared/js/position.js aktualisiert
This commit is contained in:
@@ -1,3 +1,10 @@
|
||||
/**
|
||||
* Bratonien Adventskalender – Türchen-Positionierung (2025)
|
||||
* ---------------------------------------------------------
|
||||
* Liest data-top / data-left / data-width aus jedem Türchen (door, fluegel, openfield)
|
||||
* und positioniert sie pixelgenau auf dem sichtbaren Bild.
|
||||
*/
|
||||
|
||||
function positionAllDoors() {
|
||||
const cont = document.querySelector('.kalenderbild');
|
||||
const img = cont?.querySelector('img');
|
||||
@@ -6,35 +13,73 @@ function positionAllDoors() {
|
||||
const imgRect = img.getBoundingClientRect();
|
||||
const contRect = cont.getBoundingClientRect();
|
||||
|
||||
// Normale Türen + Openfields
|
||||
document.querySelectorAll('.door, .openfield').forEach(el => {
|
||||
const topPct = parseFloat(el.dataset.top) || 0;
|
||||
const leftPct = parseFloat(el.dataset.left) || 0;
|
||||
const widthPct = parseFloat(el.dataset.width) || 10;
|
||||
// === Normale Türen (.door)
|
||||
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 widthPx = imgRect.width * (widthPct / 100);
|
||||
const heightPx = widthPx; // quadratisch
|
||||
const w = imgRect.width * (widthPct / 100);
|
||||
|
||||
el.style.position = 'absolute';
|
||||
el.style.width = widthPx + 'px';
|
||||
el.style.height = heightPx + 'px';
|
||||
el.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
|
||||
el.style.left = (imgRect.width * (leftPct / 100) + (contRect.width - imgRect.width) / 2) + 'px';
|
||||
door.style.position = 'absolute';
|
||||
door.style.width = `${w}px`;
|
||||
door.style.height = `${w}px`;
|
||||
door.style.top = `${imgRect.top - contRect.top + imgRect.height * (topPct / 100)}px`;
|
||||
door.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 = door.style.top;
|
||||
field.style.left = door.style.left;
|
||||
}
|
||||
});
|
||||
|
||||
// Flügel: data-width = bereits halbe Breite → NICHT nochmal teilen!
|
||||
// === Flügel (.fluegel.left und .fluegel.right)
|
||||
document.querySelectorAll('.fluegel').forEach(fluegel => {
|
||||
const day = fluegel.dataset.day;
|
||||
const topPct = parseFloat(fluegel.dataset.top) || 0;
|
||||
const leftPct = parseFloat(fluegel.dataset.left) || 0;
|
||||
const widthPct = parseFloat(fluegel.dataset.width) || 5;
|
||||
const widthPct = parseFloat(fluegel.dataset.width) || 6;
|
||||
|
||||
const widthPx = imgRect.width * (widthPct / 100);
|
||||
const heightPx = widthPx * 2; // volle Türhöhe
|
||||
const w = imgRect.width * (widthPct / 100);
|
||||
const h = w * 2;
|
||||
|
||||
fluegel.style.position = 'absolute';
|
||||
fluegel.style.width = widthPx + 'px';
|
||||
fluegel.style.height = heightPx + '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';
|
||||
fluegel.style.width = `${w}px`;
|
||||
fluegel.style.height = `${h}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`;
|
||||
});
|
||||
}
|
||||
|
||||
// === Openfields für Flügel
|
||||
document.querySelectorAll('.openfield').forEach(field => {
|
||||
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) || 12;
|
||||
|
||||
const w = imgRect.width * (widthPct / 100);
|
||||
const h = w * 2;
|
||||
|
||||
field.style.position = 'absolute';
|
||||
field.style.width = `${w}px`;
|
||||
field.style.height = `${h}px`;
|
||||
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`;
|
||||
});
|
||||
}
|
||||
|
||||
window.addEventListener('load', positionAllDoors);
|
||||
window.addEventListener('resize', positionAllDoors);
|
||||
window.addEventListener('orientationchange', () => {
|
||||
setTimeout(positionAllDoors, 300);
|
||||
});
|
||||
document.addEventListener('visibilitychange', () => {
|
||||
if (document.visibilityState === 'visible') {
|
||||
setTimeout(positionAllDoors, 200);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user