I love developing usable and rich web applications using the latest techniques available.

Fixed (sticky) toolbars with jQuery

January 19, 2010

Here’s a little snippet of code that let’s you create sticky toolbars like they use in Magento for example :)

Include this in your js somewhere and then just call $(‘.toolbar’).fixed();

$.fn.fixed = function() {
    return this.each(function() {
        var header = $(this);
        var offset = header.offset();
        var floater = $('<div></div>').append(header.clone(true));
        floater.hide().appendTo(document.body).addClass('floating-toolbar');

        $(window).scroll(function() {
            var s = $(this).scrollTop();

            if (s > offset.top) {
                header.css('visibility', 'hidden');
                floater.show();
            } else {
                floater.hide();
                header.css('visibility', 'visible');
            }
        });
    });
};

And here’s some css to make it work.

.floating-toolbar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    opacity: 0.85;
}
Bookmark and Share
Filed under: Javascript

2 Comments »

  1. I can’y get it to work unfortunately. This may be e Noob question but how do i call the jquery function. Does anybody have a working example?

    20/06/2010 @ 11:42 am — Dirk-Jan
  2. i quote:


    Include this in your js somewhere and then just call $(‘.toolbar’).fixed();

    Should just work

    24/06/2010 @ 6:23 pmEelco Wiersma

Leave a comment