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
            const $header = jQuery('.elementor-location-header .elementor-sticky');
            const $body = jQuery('body');
            
            if ($header.length) {
                let headerHeight = 0;
				
				if (jQuery('.elementor-location-header .elementor-sticky').length != 0) headerHeight += $header.outerHeight();
                // Si la clase admin-bar existe, sumamos 32px
                if ($body.hasClass('admin-bar')) {
                    headerHeight += 32;
                }
                // 1. Insertamos el valor como una propiedad de datos en el Body
                $body.attr('data-header-height', headerHeight + 'px');
                // 2. Insertamos la variable CSS en el :root
                document.documentElement.style.setProperty('--header-height', headerHeight + 'px');
                
                console.log('Altura calculada (con admin-bar si aplica): ' + headerHeight + 'px');
            }
        }
        // Ejecutar al cargar
        updateHeaderHeight();
        // Re-calcular si el usuario cambia el tamaño de la ventana
        jQuery(window).on('resize', function() {
            updateHeaderHeight();
        });
    });
});

Comments

Add a Comment