Files

129 lines
4.4 KiB
JavaScript

/* =====================================================================
COLLINA SYSTEMS — FOOTER (JavaScript)
- Rotación animada de la palabra del slogan ("poderoso / importante / ...")
- Relojes analógicos en vivo por zona horaria (data-tz)
Inclúyelo al final del <body>: <script src="footer.js"></script>
Es autoejecutable y no requiere dependencias.
===================================================================== */
(function () {
function initFooter() {
/* === FOOTER — rotating slogan word ================================ */
(function () {
var container = document.querySelector('.footer__slogan-words');
var words = container
? Array.prototype.slice.call(container.querySelectorAll('.footer__slogan-word'))
: [];
if (words.length < 2) return;
/* ---- Auto-fit: pick a single font-size that fits the longest word ---- */
function fitWords() {
var cw = container.clientWidth;
if (!cw) return;
var REF = 200; // measurement reference size in px
var maxNatural = 0;
words.forEach(function (w) {
var prevSize = w.style.fontSize;
w.style.fontSize = REF + 'px';
var natural = w.scrollWidth;
if (natural > maxNatural) maxNatural = natural;
w.style.fontSize = prevSize;
});
if (!maxNatural) return;
var fitSize = (cw / maxNatural) * REF;
var line = document.querySelector('.footer__slogan-line');
var lineSize = line ? parseFloat(getComputedStyle(line).fontSize) : Infinity;
var finalSize = Math.min(fitSize, lineSize);
words.forEach(function (w) {
w.style.fontSize = finalSize + 'px';
});
container.style.height = finalSize + 'px';
}
fitWords();
window.addEventListener('resize', function () {
window.clearTimeout(container.__fitT);
container.__fitT = window.setTimeout(fitWords, 80);
});
/* ---- Cross-fade / slide rotation ---- */
var idx = 0;
var INTERVAL = 2400;
var TRANSITION = 600;
function next() {
var current = words[idx];
idx = (idx + 1) % words.length;
var upcoming = words[idx];
current.removeAttribute('data-active');
current.setAttribute('data-leaving', 'true');
upcoming.removeAttribute('data-leaving');
void upcoming.offsetWidth;
upcoming.setAttribute('data-active', 'true');
window.setTimeout(function () {
current.removeAttribute('data-leaving');
}, TRANSITION + 50);
}
window.setInterval(next, INTERVAL);
})();
/* === FOOTER — live analog clocks per timezone ===================== */
(function () {
var clocks = Array.prototype.slice.call(document.querySelectorAll('.footer__branch[data-tz]'));
if (!clocks.length) return;
function getTzTime(tz) {
try {
var fmt = new Intl.DateTimeFormat('en-US', {
timeZone: tz,
hour12: false,
hour: '2-digit',
minute: '2-digit',
second: '2-digit'
});
var parts = fmt.formatToParts(new Date());
var h = 0, m = 0, s = 0;
parts.forEach(function (p) {
if (p.type === 'hour') h = parseInt(p.value, 10) % 24;
if (p.type === 'minute') m = parseInt(p.value, 10);
if (p.type === 'second') s = parseInt(p.value, 10);
});
return { h: h, m: m, s: s };
} catch (e) {
var d = new Date();
return { h: d.getHours(), m: d.getMinutes(), s: d.getSeconds() };
}
}
function tick() {
clocks.forEach(function (branch) {
var tz = branch.getAttribute('data-tz');
var t = getTzTime(tz);
var hourDeg = ((t.h % 12) + t.m / 60) * 30;
var minDeg = (t.m + t.s / 60) * 6;
var hourEl = branch.querySelector('.footer__clock-hand--hour');
var minEl = branch.querySelector('.footer__clock-hand--minute');
if (hourEl) hourEl.style.transform = 'rotate(' + hourDeg.toFixed(2) + 'deg)';
if (minEl) minEl.style.transform = 'rotate(' + minDeg.toFixed(2) + 'deg)';
});
}
tick();
window.setInterval(tick, 15000);
})();
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', initFooter);
} else {
initFooter();
}
})();