Creating a Floating HTML Menu Using jQuery and CSS

Tutorial Details
  • Difficulty: Beginner
  • Completion Time: 30 Minutes

For all of us who deal with long web pages and need to scroll to the top for the menu, here’s a nice alternative: floating menus that move as you scroll a page. This is done using HTML, CSS and jQuery, and it’s fully W3C-compliant.

Demo and Source

You can see a demo of this tutorial by clicking on the “Demo” button above. For the source download, please note that the jQuery library, Dimensions plugin, and Eric Meyer’s Reset code are not in the ZIP file.

What We’re Building

This tutorial covers how to create a “floating menu” using HTML, CSS, and jQuery. To reiterate, a floating menu stays visible even if you scroll down a web page. They’re animated, so they move up and down as you scroll the browser window up or down. I am going to show you how to make a floating menu using jQuery and CSS, and hopefully make some new jQuery disciples :D.

Before we continue to the coding steps, have a look at the two screen snaps below. The first shows a web page with a floating menu at top right. Of course, you can’t tell it’s floating until you see it live and actually scroll the page. So look at the second snapshot, and you can see that the menu has moved.

Step 1

Let’s start with the HTML markup for a nice menu consisting of three sub-menus:

    <div id="floatMenu">
        <ul>
            <li><a href="#" onclick="return false;"> Home </a></li>
        </ul>
        
       <ul>
            <li><a href="#" onclick="return false;"> Table of content </a></li>
            <li><a href="#" onclick="return false;"> Exam </a></li>
            <li><a href="#" onclick="return false;"> Wiki </a></li>
        </ul>
        
        <ul>
            <li><a href="#" onclick="return false;"> Technical support </a></li>
        </ul>
    </div>

This is the basic markup we will use. The main part in this bit of HTML is the <div id=”floatMenu”>…</div> in Line 01, which encapsulates the whole menu. The three lists are only used to demonstrate structure, which can be modified to suit your needs. In this case, there are three sections to the menu, as represented by three unordered HTML lists.

As a matter of habit, I disable the click on dummy links (href=”#”). Just to be sure that a click on a dummy link doesn’t send the page back to the top, there is also an onclick=”return false;” in <a href>. This method allows to add menu item features such as lightboxing – something that requires the page to stay at its current vertical position when the user clicks on a menu link.

Step 2

Now we need some CSS rules to skin and position the menu. (I used Eric A. Meyer’s CSS Reset, so that’s why there is no margin:0 or padding:0 on the ul element):

body {
    background-color:#000;
    height:2000px;
    color:#ccc;
    font:10px "Lucida Grande", "Lucida Sans", "Trebuchet MS", verdana, sans-serif;
}
#floatMenu {
    position:absolute;
    top:150px;
    left:50%;
    margin-left:235px;
    width:200px;
}
#floatMenu ul {
    margin-bottom:20px;
}
#floatMenu ul li a {
    display:block;
    border:1px solid #999;
    background-color:#222;
    border-left:6px solid #999;
    text-decoration:none;
    color:#ccc;
    padding:5px 5px 5px 25px;
}

The body height (Line 03, above) has been set only to get enough room for our menu to scroll up and down with the page. This should be removed in a real case scenario. The two other things to take note of are the position:absolute (Line 08) and the left:50% (Line 10), both in the #floatMenu CSS rule (Line 07), above.

The “position” attribute is used when you need to remove an element from the flow of the document and keep it at a precise place in your page. If you use the text zoom function of your browser, an element with absolute positioning will not move, even if the text around it increases in size.

The “left” attribute is used to position the specific div element horizontally. The value needs to be defined as a percentage in the case that we want a centered design. With a 50% value, the left side of the container is positioned in the middle of the page. To position it left or right we need to use the “margin-left” attribute (Line 11), with a negative value for an offset to the left and a positive one for an offset to the right.

The others elements in the above stylesheet rules customize the visual design.

Step 3

Now we have a menu of three sections positioned in the upper right hand side of the page. To enhance the menu item roll-over effect, let’s add style classes menu1, menu2 and menu 3 to each menu section, respectively (to each <ul> element). We will have 3 distinct sub-menus using our 3 <ul> tags. The code below is a modification of the HTML code shown in Step 1 above:

    <div id="floatMenu">
        <ul class="menu1">
            ...
        </ul>
        
       <ul class="menu2">
           ...
        </ul>
        
        <ul class="menu3">
          ...
        </ul>
    </div>

Now let’s define some CSS hover-based roll-over effects, which will be different for each menu section.

    #floatMenu ul.menu1 li a:hover {
        border-color:#09f;
    }
    #floatMenu ul.menu2 li a:hover {
        border-color:#9f0;
    }
    #floatMenu ul.menu3 li a:hover {
        border-color:#f09;
    }

Now each menu section will display a different color when the mouse hovers over a menu item. If you like, you can also add rules for other menu link states using :link, :visited, :hover and :active pseudo classes. The order in which you should write them can be easily memorized like this: LoVe and HAte, where the capitalized letters represents the first letter of each state.

Step 4

We’ve got a nice looking menu and could stop here, but we do want that floating menu, so it’s time to add some jQuery. You’ll need to download the jQuery library and the Dimensions plugin. This plugin will be used to grab information about the browser’s window (width, height, scroll, etc.). You can link to both bits of jQuery code from your HTML file in the <head>…</head> section. Just remember to change the URL path according to where on your server you place the jQuery library and plugin files.

    <script language="javascript" src="jquery.js"></script>
    <script language="javascript" src="jquery.dimensions.js"></script>

We’ll need some custom jQuery code as well, so start a new <script> section, also within the <head>…</head> section of your HTML document:

    <script language="javascript">
		...
    </script>

Add the following jQuery code inside the the <script> section:

    $(document).ready(function(){
        // code will go here
    }); 

The $(document).ready() function is similar to the window.onLoad but improved. With the window.onLoad function, the browser has to wait until the whole page (DOM and display) is loaded. With the $(document).ready() function, the browser only waits until the DOM is loaded, which means jQuery can start manipulating elements sooner.

Step 5

We need a listener for the “scroll page” window event. Our custom jQuery script now looks like this:

	
    $(document).ready(function(){
        $(window).scroll(function () { 
            // code will go here
        });
    }); 

A listener is an event handler waiting on standby for a particular window event to happen – in this a page scroll up or down.

Step 6

Since our menu will “float” as the page is scrolled, we need to track its initial position. Instead of hard-coding that into the jQuery, we’ll read it’s position using the Dimensions jQuery plugin, then use the retrieved value. We will do the same with the name of our menu. Let’s add two variable definitions (Lines 01, 02) so that our code now looks like this:

    var name = "#floatMenu";
    var menuYloc = null;
	
    $(document).ready(function(){
        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
        $(window).scroll(function () { 
            // code will go here
        });
    }); 

Lines 01 and 02 define variables “name” and “menuYloc”. Line 05 sets the value of “menuYloc”. The “name” variable will be used to reference our floating menu. The “menuYloc” variable will contain the original vertical position of our menu.

Let’s look at how the value of menuYloc is set in Line 05. This statement is an example of jQuery’s powerful function-chaining. First we read the “top” attribute value from the CSS rules of our menu element (which is “150px”, set in Step 2). Then we strip off the “px” string at the end, since we only need the “150″ part. To do this, the jQuery function call .css(“top”) first finds the value of the top attribute for the menu. (This attribute was set in Line 09 of the code in Step 2, above.) That results in retrieving the value “150px”. Then the .indexOf() function finds where the “px” in “150px” starts, and the .substring() function ensures we save everything before the “px”. The .parseInt() function turns the string “150″ into an numeric integer value.

Step 7

We now arrived at the fun part of this tutorial: animating the menu to make it “float”. To do this, we need to determine how far the page has scrolled in pixel dimension. We have the original menu location stored in variable “menuYloc”. We need the offset of the scroll bar, which we can get from the command $(document).scrollTop(), defined in the Dimensions jQuery plugin. After grabbing the offset we can add the animate command. Lines 07 and 08, below, show the new code:

    var name = "#floatMenu";
    var menuYloc = null;
	
    $(document).ready(function(){
        menuYloc = parseInt($(name).css("top").substring(0,$(name).css("top").indexOf("px")))
        $(window).scroll(function () { 
            var offset = menuYloc+$(document).scrollTop()+"px";
            $(name).animate({top:offset},{duration:500,queue:false});
        });
    }); 

The variable “offset”, in Line 07 above, contains the difference between the original location of the menu (menuYloc) and the scroll value ($(document).scrollTop()), in pixel measurement. To make it work as a CSS rule, we add the necessary measurement unit, “px”, after the numeric value. Now we can apply the vertical offset, as calculated, to position the menu and thus making it move.

To make it all look nicer, let’s make use of jQuery’s animation options. We’ve stored the menu name in the variable “name” and can recall it when needed, to use it along with the .animate() function. The animate function requires two parameters: (1) the style properties, and the (2) animation options. In this tutorial, we just need to animate the “top” CSS property, but to specify additional parameters, separate each property:value pair with a comma (,).

We’re using two parameters here. The “duration” is the length of the animation
in milliseconds, and the “queue” is a list of all positions we want our object to be animated to. Since we only want to animate our object to its final location (the browser’s current scroll location), we set “queue” to false.

We should now have a functioning floating menu.

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.bradenkeith.com Braden Keith

    @The Dude
    Yeah you’re old school. Lol. But no, with some color changes you could make this work in a professional setting.

  • http://talkingtofu2.iblogger.org/blog/ Taylor Satula

    Wow like the link to cssplay. When I saw this i thought “Wow somebody is not good at css” thanks for saying about the css thing. JS free too.!

  • http://talkingtofu2.iblogger.org/blog/ Taylor Satula

    Ok well not bad but not a css wizard. That came out wrong

  • http://workingonit... Jonas

    OK – I am designing my portfolio site (I work mainly in print). Is it possible to use this “menu” as a thumbnail gallery.

    So, when the image is clicked it would scroll the whole page vertically?

  • Khaotic

    wow very nice i will definitely use this =]

  • somi

    easy code , but nice float menu ~~

  • http://klangkunst-ping jo

    will it work with FF3??? at the moment version FF 3.0.1 no!!!!!
    thx

  • http://klangkunst-ping jo

    why does it work for your page???

  • http://www.xp-internet.com Xavier

    Hi,

    Very helpful and straightforward, thank you so much.

    I just do not understand why you use indexOf() on line 05, since parseInt() will get rid of “px” anyway, unless I am missing something. I implemented your solution without indexOf() and works like a charm!

  • http://www.windows7themes.com Windows Themes

    nice one but needs improvement. Thanks

  • http://www.smallwebsitehost.com web hosting

    I downloaded the script, but it is not working. :( It seems that it is just me who have this problem. I tried with IE7 and firefox 3.0.1

  • http://www.egyptwebdesign.com/ egypt web design

    wow nice menu

  • http://URL(Optional) Cliff

    One thing about this setup though is that it works well in a fixed-width layout where you can position the menu absolutely regardless of how wide the user’s browser window is.

    However, with a fluid-width layout (or sometimes called “liquid-width”), the left:50% code is probably going to be inadequate.

    The menu then “floats” to a percentage value meaning that the menu is not fixed to the right side of the window and drifts away toward the left as the window is resized wider.

    This makes it hard to set a menu inside of a right sidebar column in a fluid-width layout.

  • http://www.givle.com Evolve

    This guy cracks the problem with no jQuery and the simplist of CSS code. Works with FF3, ie6, mozilla, opera etc etc..

    His site here: http://www.cssplay.co.uk/layouts/fixed.html

    Basically this is the code:

    Make a div for the floating section in html.

    This is my floaty text, it floats YAY!
    Replace this code with your floating links, text, images code

    In your CSS add the following:
    body{
    height:100%;
    overflow-y:auto;
    }

    #menu {display:block; top:10px; left:170px; width:130px; position:fixed; border:1px solid #888; padding:10px; }

    * html #menu {position:absolute;}

    add this as a ie6 hack in your HTML header tag.

    /**/

    IT WORKS! NO HASSLES. SURE IT COULD BE ENHANCED BUT HEY ITS SIMPLE.

  • Pingback: jQuery: criando um menu flutuante simples « Alex Blog Estudos

  • http://theadmin.org Eric Davis

    Thanks, this worked great. I didn’t use it as a menu but as a counter to keep track of how many rows a user has selected (e.g. You have selected 12 of 134 items).

  • MissANN

    wow!!!! this is what i’ve been looking for!!! thnk u very much for this tutorial… ^___^
    –ann

  • Pingback: 13 Excellent Tutorials On Creating jQuery Navigation Menu | DeveloperFox

  • http://unofficial.trunkmedia.co.nz Scott

    If you don’t want to have to position the menu element with CSS to start with (ie absolute top (top:20px;)) then you can change this line
    menuYloc = parseInt($(name).css(“top”).substring(0,$(name).css(“top”).indexOf(“px”)))
    to
    menuYloc = parseInt($(name).offset().top);
    It’s the jquery way.
    Works for me in IE6, Firefox 2, Opera 9.27

  • Pingback: 13 Excellent Tutorials On Creating jQuery Navigation Menu | Pedram Development

  • prsanth

    very nice! its easy to use and understand!!

    Thank you!

  • Pingback: jQuery menu/ navigation tutorials | Web Design Blog x2interactive. Ένα blog για το Internet και το Web Design

  • Pingback: .: Darivel´s Blogg :. » Blog Archive

  • elusive

    “How about when you want this menu to stop scrolling down at a certain point? Mine keeps going into the footer. Any suggestions?”

    I am having the same issues when viewed in different resolutions. Can this be done?

  • John

    Totally Tubular Dude

  • Mario Di Vece

    Nice tutorial! In mootools 1.2, you’de do exactly the same except for step 7 which would be:
    window.addEvent(‘domready’, function(event) {
    window.addEvent(‘scroll’, function(ev) {
    var offset = $(document).getScrollTop();
    $(‘testBasket’).tween(‘top’, offset + ‘px’);
    });
    });

  • Pingback: Floating | keyongtech

  • Pingback: WebAir Blog » Blog Archive » 17 “Must see” Menu in jQuery

  • Pingback: Werbebanner mitscrollen lassen - XHTMLforum

  • Adam

    thanks Mario…. I have movement with Mootools!!

    but… can u help me out. The moment the page starts scrolling the div jumps to the ‘top’ of the wrapper div container rather than the ‘top’ absolute positioning of the specified div.

  • Pingback: 25 jQuery Tutorials for Improved Navigation Menus

  • http://cepoko.com Widodo

    Thanks for your tutorial. I need it.. I will use on my on my wordpress themes.

  • alex

    Doesn’t work with safari ?

  • alex

    sorry for the comment above, just checked it and it works, but i was messing with the source.

  • http://www.newalexandria.org shpoffo

    You need to properly define your DTD for Firefox 3+ to correctly compute .scrollTop() Safari will then break, in all cases I found. Use:

    function getScroll () {
    if ( window.pageYOffset > $(“html,body”).scrollTop() ) {
    return window.pageYOffset;
    } else {
    return $(“html,body”).scrollTop() ;
    }
    }

    also search my username and scrollTop to find other forum discussion.

  • suresh

    Gud floating Menu, i found one mis alignment in the menu. when the user minimize the browser window, then menu is aligned properly, according to window size, the menu should position the same . can u tell me how to achieve this one ?

  • suresh

    Gud floating Menu, i found one mis alignment in the menu. when the user minimize the browser window, then menu is not aligned properly, according to window size, the menu should position the same . can u tell me how to achieve this one ?

  • Lio

    Hello,
    Thank you for this tutorial. I followed it and it works. I have a problem though, and I don’t know if you could help me. I have a rather long menu and its top position is 320px. So you cannot fully see it on a 1024/768 screen resolution. When you start scrolling, one can never see the lower part of the menu. It keeps staying at 320px from top. Can it be made to have a higher top position after starting scrolling, so that the menu might be seen in it’s entirety?
    Thank you for your help.

    • Jon

      I have the same problem as Lio and have tried, but failed, to come up with a solution. Has anyone got any suggestions? Thanks!

    • http://www.my-bath.com greg walker

      did you ever find out how to fix this? Most likely using an if/then command?

  • http://www.potamocheri.eu/blog Ted

    How can I stop scrolling when the floatMenu is > the div#footer?

  • http://100mitra.blogspot.com saumitra

    great post

  • http://hackermojo.com Glenn

    Look at apple’s summary block on this page: http://store.apple.com/us/configure/MB881LL/A?mco=MzE2NjMyOA

    It slides within it contraints without the tweening of position. How? Can jquery do this?

  • Mr.adny

    very nice.

  • lili

    could this menu also float left and right for a horizontally scrolling page? have tried as it is but it doesn’t float L/R.

  • http://tuts.cgbaran.com CgBaran Tuts

    Great tutorial thanks

  • Melissa

    What a great tutorial! You have explained everything very well, even to someone like me who is brand new to CSS and knows nothing about Java. Thank you so much.

  • Pingback: Creating a Floating HTML Menu Using jQuery and CSS « Dogfeeds——IT Telescope

  • Pingback: Web Design and Development Tutorials | Serene Destiny

  • Ben

    Helpful tutorial.

    Pretty useful for shopping carts you want to scroll with you down the page, but don’t want to use position:fixed in case a use expands their browser window.

    I guess Apple uses something like this for their shopping basket?

  • http://www.designdazzling.com Ejaz ahmad

    Hi

    i checked this code on all of browsers… in mozilla its not working fine .. menu having space from in IE but not in mozzilla . except this script is very nice

  • http://ayosini.com iklan baris

    Nice floating Menu. Keep up the great work :)