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

58 lines
2.3 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 Türchen-Positionierung (2025)
* ---------------------------------------------------------
* Liest data-top / data-left / data-width aus jedem Element
* und positioniert .door, .fluegel, .openfield korrekt über dem Bild.
*/
function positionAllDoors() {
const cont = document.querySelector('.kalenderbild');
const img = cont?.querySelector('img');
if (!cont || !img) return;
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;
const widthPx = imgRect.width * (widthPct / 100);
const heightPx = widthPx; // quadratisch
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';
});
// Flügel: jeweils halbe Breite, doppelte Höhe (nicht quadratisch!)
document.querySelectorAll('.fluegel').forEach(fluegel => {
const topPct = parseFloat(fluegel.dataset.top) || 0;
const leftPct = parseFloat(fluegel.dataset.left) || 0;
const widthPct = parseFloat(fluegel.dataset.width) || 10;
const widthPx = imgRect.width * (widthPct / 100) / 2;
const heightPx = widthPx * 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';
});
}
window.addEventListener('load', positionAllDoors);
window.addEventListener('resize', positionAllDoors);
window.addEventListener('orientationchange', () => {
setTimeout(positionAllDoors, 300);
});
document.addEventListener('visibilitychange', () => {
if (document.visibilityState === 'visible') {
setTimeout(positionAllDoors, 200);
}
});