Home / Widgets / Determinar altura del header
Duplicate Snippet

Embed Snippet on Your Site

Determinar altura del header

Code Preview
js
jQuery(window).on('elementor/frontend/init', function() {
    // Elementor tiene su propio evento de carga de componentes
    jQuery(window).on('load', function() {
        
        function updateHeaderHeight() {
            // Seleccionamos el header (ajusta el selector si es necesario)
            const $header = jQuery('header, .elementor-location-header');
            
            if ($header.length) {
                const headerHeight = $header.outerHeight();
                // 1. Insertamos el valor como una propiedad de datos en el Body
                jQuery('body').attr('data-header-height', headerHeight + 'px');
                // 2. Opcional: Insertamos una variable CSS en el :root 
                // Esto es súper útil para usarlo en tu CSS como var(--header-height)
                document.documentElement.style.setProperty('--header-height', headerHeight + 'px');
                
                console.log('Altura del Header capturada: ' + headerHeight + 'px');
            }
        }
        // Ejecutar al cargar
        updateHeaderHeight();
        // Re-calcular si el usuario cambia el tamaño de la ventana
        jQuery(window).resize(function() {
            updateHeaderHeight();
        });
    });
});

Comments

Add a Comment