avatar
Untitled

Guest 118 5th Mar, 2024

JAVASCRIPT 1.40 KB
                                           
                         function toggleDescription() {
    const category = document.querySelector('body.tax-product_cat');
    const description = document.querySelector('.term-description');
    
    if (category && description) {
        let paragraphs = description.querySelectorAll('p');
        let array = [];
        let minHeight = 0;
        let maxHeight = description.offsetHeight;
        
        for (let i = 0; i < paragraphs.length; i++) {
            array.push(paragraphs[i]);
            minHeight = minHeight + paragraphs[i].offsetHeight + 14;
            
            if (paragraphs[i].textContent == 'Czytaj więcej') {
                break;
            }
        }
        
        description.dataset.minHeight = minHeight;
        description.dataset.maxHeight = maxHeight;
        
        description.style.maxHeight = minHeight + 'px';
        
        let button = array[array.length - 1];
        button.classList.add('description-toggler');
        
        button.addEventListener('click', () => {
            if (description.offsetHeight == parseInt(description.dataset.maxHeight)) {
                description.style.maxHeight = description.dataset.minHeight + 'px';
            } else {
                description.style.maxHeight = description.dataset.maxHeight + 'px';
            }
        });
    }
}
window.onload = toggleDescription();
                      
                                       
To share this paste please copy this url and send to your friends
RAW Paste Data
Recent Pastes
Ta strona używa plików cookie w celu usprawnienia i ułatwienia dostępu do serwisu oraz prowadzenia danych statystycznych. Dalsze korzystanie z tej witryny oznacza akceptację tego stanu rzeczy.
Wykorzystywanie plików Cookie
Jak wyłączyć cookies?
ROZUMIEM