El error que mencionas (en la línea 88) ocurre porque estás usando las variables topContent y topContentBottomPosition dentro de la función updatePosition(), pero estas variables no están definidas en el ámbito global ni se pasan como parámetros, por lo tanto, son undefined cuando la función intenta acceder a ellas.
❌ Problema: Referencia a variables no definidas
function updatePosition() {
const rect = topContent.getBoundingClientRect(); // ← topContent es undefined
topContentBottomPosition = rect.bottom; // ← topContentBottomPosition es undefined
// ...
}
Aunque declaraste estas variables dentro del evento load, su ámbito está limitado al callback de ese evento, por lo que updatePosition() no puede verlas.
✅ Solución: Declarar las variables en el ámbito global
Debes mover la declaración de topContent y topContentBottomPosition al ámbito global (fuera de cualquier función), para que updatePosition() pueda acceder a ellas.
✅ Código corregido (solo cambios clave):
/**
* Main
*/
// Variables globales
let topContent = null;
let topContentBottomPosition = 0;
// Ejecutar al cargar la página
window.addEventListener("DOMContentLoaded", checkDeviceCompatibility);
window.addEventListener("load", function (event) {
const device = getDeviceInfo();
topContent = document.getElementById('top-content'); // Asignar a variable global
if (topContent) {
updatePosition(); // Primera ejecución
}
});
window.addEventListener('resize', function () {
if (topContent) {
updatePosition();
}
});
/**
* getDeviceInfo function
* Retorna: {
* type: "xsmall"|"small"|"medium"|"large"|"xlarge",
* width: number,
* height: number,
* orientation: "portrait"|"landscape",
* aspectRatio: number
* }
*/
function getDeviceInfo() {
const width = window.innerWidth;
const height = window.innerHeight;
const aspectRatio = width / height;
const orientation = width > height ? "landscape" : "portrait";
let type;
if (width < 360) {
type = "xsmall";
} else if (width <= 600) {
type = "small";
} else if (width <= 992) {
type = "medium";
} else if (width <= 1200) {
type = "large";
} else {
type = "xlarge";
}
return {
type,
width,
height,
orientation,
aspectRatio
};
}
// Ejemplo de uso:
const device = getDeviceInfo();
console.log(device);
/**
* Verifica si el dispositivo es compatible
*/
function isDeviceSupported() {
return getDeviceInfo().type !== "xsmall";
}
/**
* Muestra error y deshabilita la app en dispositivos no soportados
*/
function checkDeviceCompatibility() {
if (!isDeviceSupported()) {
const unsupportedEl = document.getElementById("unsupported-device");
const appContentEl = document.getElementById("app-content");
if (unsupportedEl) {
unsupportedEl.classList.remove("w3-hide");
}
if (appContentEl) {
appContentEl.classList.add("w3-hide");
}
throw new Error("Dispositivo no soportado: viewport < 360px");
}
}
/**
* Reajusta la posición de middle-content según el header
*/
function updatePosition() {
if (!topContent) return;
const rect = topContent.getBoundingClientRect();
topContentBottomPosition = rect.bottom;
console.log('Posición inferior de top-content:', topContentBottomPosition + 'px');
const middleContent = document.getElementById('middle-content');
if (!middleContent) return;
// Asegurar posición correcta si el header es fijo
if (getComputedStyle(topContent).position === 'fixed') {
middleContent.style.marginTop = `${rect.height}px`;
} else {
middleContent.style.marginTop = ''; // Reset si no es fijo
}
}