Files
thalim-theme/js/fitPostCardTitle.js
2026-05-12 23:33:46 +02:00

44 lines
1.4 KiB
JavaScript

document.addEventListener('DOMContentLoaded', function () {
function fitTitles() {
var cards = document.querySelectorAll('.post-card');
cards.forEach(function (card) {
var h2 = card.querySelector('.gradient-container h2');
if (!h2) return;
var container = h2.closest('.gradient-container');
var maxHeight = container.clientHeight;
var fontSize = parseFloat(window.getComputedStyle(h2).fontSize);
var minSize = 12;
h2.style.fontSize = '';
fontSize = parseFloat(window.getComputedStyle(h2).fontSize);
while (h2.scrollHeight > maxHeight && fontSize > minSize) {
fontSize -= 1;
h2.style.fontSize = fontSize + 'px';
}
});
}
window.fitPostCardTitles = fitTitles;
fitTitles();
var resizeTimer;
window.addEventListener('resize', function () {
clearTimeout(resizeTimer);
resizeTimer = setTimeout(fitTitles, 200);
});
// Re-fit after infinite scroll loads new cards
var observer = new MutationObserver(function (mutations) {
mutations.forEach(function (m) {
if (m.addedNodes.length) fitTitles();
});
});
var grid = document.getElementById('post-grid');
if (grid) {
observer.observe(grid, { childList: true });
}
});