Search code examples
jqueryhtmlcsswordpresssticky

Wordpress Sticky navbar when the admin bar is present


I used this sticky jQuery plugin in my site for the top navbar. jQuery Sticky

It works correctly, but when the WP admin bar is present that cover the top navbar. I tried to dependency the stickiness to the admin bar:

jQuery(document).ready(function($){                                                        
    if ( $("#wpadminbar").length ) {
                $("#navbar").sticky({topSpacing:"28px"}); 
                    } else { $("#navbar").sticky({topSpacing:0});
                           }                                   
                 });

but this is immediately stuck the navbar to the top not only when scrolling. I appreciate any assistance. Thanks


Solution

  • Can't just adjust navbar's margin-top?

    I'm not using the Sticky plugin but I've a similar problem and the code below solve.

    (function($) {
        'use strict';
    
        $(document).ready(function() {
            // if adminbar exist (should check for visible?) then add margin to our navbar
            var $wpAdminBar = $('#wpadminbar');
            if ($wpAdminBar.length) {
                $('div.navbar').css('margin-top',$wpAdminBar.height()+'px');
            }
        });
    })(jQuery);