adventskalender/shared/js/position.js aktualisiert
This commit is contained in:
@@ -1,55 +1,34 @@
|
|||||||
/**
|
/**
|
||||||
* Bratonien Adventskalender – Türchen-Positionierung
|
* Bratonien Adventskalender – Türchen-Positionierung (2025)
|
||||||
* --------------------------------------------------
|
* ---------------------------------------------------------
|
||||||
* Dieses Skript sorgt dafür, dass sich jedes .door-Element
|
* Liest data-top / data-left / data-width aus jedem .door
|
||||||
* exakt relativ zum sichtbaren Kalenderbild positioniert.
|
* und positioniert die Elemente exakt auf dem sichtbaren Bild.
|
||||||
*
|
|
||||||
* Autor: Bratonien Development
|
|
||||||
* Stand: 2025
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
function positionAllDoors() {
|
||||||
* Positioniert ein einzelnes Türchen auf Basis prozentualer Angaben.
|
const container = document.querySelector('.kalenderbild');
|
||||||
*
|
const img = container?.querySelector('img');
|
||||||
* @param {string} selector - CSS-Selektor des Türchens (z. B. ".door" oder ".door[data-day='1']")
|
if (!container || !img) return;
|
||||||
* @param {number} topPct - vertikale Position in Prozent (0–100) bezogen auf das Bild
|
|
||||||
* @param {number} leftPct - horizontale Position in Prozent (0–100) bezogen auf das Bild
|
|
||||||
* @param {number} widthPct - Breite des Türchens in Prozent der Bildbreite
|
|
||||||
*/
|
|
||||||
function positionDoor(selector, topPct, leftPct, widthPct) {
|
|
||||||
const img = document.querySelector('.kalenderbild img');
|
|
||||||
const cont = document.querySelector('.kalenderbild');
|
|
||||||
const door = document.querySelector(selector);
|
|
||||||
|
|
||||||
if (!img || !cont || !door) return;
|
|
||||||
|
|
||||||
const imgRect = img.getBoundingClientRect();
|
const imgRect = img.getBoundingClientRect();
|
||||||
const contRect = cont.getBoundingClientRect();
|
const contRect = container.getBoundingClientRect();
|
||||||
const doorWidth = imgRect.width * (widthPct / 100);
|
|
||||||
|
|
||||||
door.style.position = 'absolute';
|
// alle Türen neu berechnen
|
||||||
door.style.width = doorWidth + 'px';
|
document.querySelectorAll('.door').forEach(door => {
|
||||||
door.style.height = doorWidth + 'px';
|
const topPct = parseFloat(door.dataset.top) || 0;
|
||||||
door.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
|
const leftPct = parseFloat(door.dataset.left) || 0;
|
||||||
door.style.left = (imgRect.left - contRect.left + imgRect.width * (leftPct / 100)) + 'px';
|
const widthPct = parseFloat(door.dataset.width) || 10;
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
const doorWidth = imgRect.width * (widthPct / 100);
|
||||||
* Positioniert automatisch alle Türchen mit data-pos-Attribut.
|
|
||||||
* Beispiel:
|
door.style.position = 'absolute';
|
||||||
* <div class="door" data-day="1" data-top="20" data-left="15" data-width="10">1</div>
|
door.style.width = doorWidth + 'px';
|
||||||
*/
|
door.style.height = doorWidth + 'px';
|
||||||
function positionAllDoors() {
|
door.style.top = (imgRect.top - contRect.top + imgRect.height * (topPct / 100)) + 'px';
|
||||||
const doors = document.querySelectorAll('.door');
|
door.style.left = (imgRect.left - contRect.left + imgRect.width * (leftPct / 100)) + 'px';
|
||||||
doors.forEach(door => {
|
|
||||||
const t = parseFloat(door.dataset.top) || 0;
|
|
||||||
const l = parseFloat(door.dataset.left) || 0;
|
|
||||||
const w = parseFloat(door.dataset.width) || 10;
|
|
||||||
const selector = `.door[data-day='${door.dataset.day || ''}']`;
|
|
||||||
positionDoor(selector, t, l, w);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/* === Events === */
|
// Positionierung bei Start und Fensteränderung
|
||||||
window.addEventListener('load', positionAllDoors);
|
window.addEventListener('load', positionAllDoors);
|
||||||
window.addEventListener('resize', positionAllDoors);
|
window.addEventListener('resize', positionAllDoors);
|
||||||
Reference in New Issue
Block a user