Everything Photoshop Subscribe

Creating a Floating HTML Menu Using jQuery and CSS

In HTML / CSS, Javascript / AJAX by Jeff Dion

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.

Enjoyed this post? Your vote is always appreciated!! Delicious StumbleUpon Float Digg

Comments

Leave a Comment
  1. Nice floating Menu. Keep up the great work :)

  2. Gravatar

    Alexis Garduño

    So Beautiful:)

  3. After you scroll up to the top, the floating menu didn’t return to its original positional….

  4. Good job! A perfect example as of why jQuery makes writing Javascript so much more fun :)

    One small remark might be that it doesn’t work well in Firefox 3. As soon as you scroll the page the menu jumps to the top of the viewport, disregarding the offset. Other than that, it’s great!

  5. Gravatar

    Raanan Avidor

    Very very nice. Two problems. One: If the menu is too long (or the browse window height is too short) you will not be able to scroll down to the bottom of the menu. Two: In the RSS feed of this article when you click on the link to the demo you get a blank page.

  6. Very Nice indeed. Great Job Jeff!

  7. 403 when trying to view the amazonaws.com URL at the bottom :)

  8. Hey Raj,

    I would suggest putting the live demo link near the top of the post so people can see whether they like the effect or not before reading through the tutorial.

    Congrats on getting your first tut up :).

  9. Gravatar

    Bruce Alrighty

    This great. I have been thinking about putting something like on my next site.

    Why no download link?

  10. Awesome! Thanks a lot for this.

  11. Skellie: Collis is producing some nice “demo” buttons that we’ll be putting at the top and bottom of all tutorials.

    Bruce Almighty: I don’t know. A complete oversight on my part. I’ll get that fixed tomorrow. Apologies to all.

    Everyone else: I tested Jeff’s code on multiple new browsers (except FF 3) and it worked fine. So did the demo link (must have been an Amazon problem). Older browsers don’t support jQuery cleanly.

  12. I love nettuts, but I wish not every other tutorial was a tutorial on how to make a jQuery menu. Don’t take me wrong, the tutorials on these topics are pretty slick, but I wouldn’t mind seeing a couple more Css and HTML and Site Builds (Which are the coolest thing on the planet btw…). And what about server side things, like, PHP, database and the such, maybe some more wordpress thingies.

    Ok. I really think I’m complaining to much. Please don’t hate me Collis. :|

    Matt.

  13. Good tutorial I like the outcome

    hey do you think you guys could do some Mootools tutorials too?
    http://mootools.net/

    its a bit like jquery so I’m told

  14. Another nice effect to try :)

  15. Never been a fan of floating menus and/or long long pages however this tutorial is great to have an idea. With a bit fine-tuning it will rock

    However, as far as I see, mootols is forgotten in favor of jquery

  16. nice tutorial! sharp and clean.
    keep up the good work and thanks a lot!

  17. With jQuery 1.2.6 there’s no need to link the Dimensions plugin.

  18. Another great tutorial but I’m afraid I’m not really a fan of animated menus. They distract users from the real content and are really annoying when they overlap other content. Javascript and JQuery isn’t needed in situations like this.

    All you need to do is set a div with the css ‘position:fixed;’ You can then have a fixed menu at the top, side or bottom of the page even for browsers with no Javascript.

  19. Nice tutorial, and the effect is nice and smooth too.

  20. by the way - nothing has moved on my browser… Safari and I tried FF as well

  21. Nice floating menu with a good explanation, but it obscures some of the content in that example, which a little bit poor usability-wise.

    That’s a small point really though - something to be aware of when doing floating menus such as this.

  22. Nice tutorial. I don’t like so much the final effect, but it’s a personal opinion.

  23. Nice tutorial! I want more and more NetTuts tutorials! :P

    David Carreira

  24. Good Tutorial but a bug when you scroll back to top the menü ist not in the start position.

  25. that’s pretty cool i like it

  26. Nice Tutorial, but when you are coming back on the top of the page the menu is not coming back into the normal position.

  27. Bug with Firefox 3 please fix.

    It doesn’t stick back to the original position.

    Come on this should have been tested first.

  28. Gravatar

    Stephen Orr

    2 problems:

    1. In the first listing, you’re missing the closing quote on the onclick attribute of each link.
    2. In the latest versions of jQuery, the Dimensions plugin has been integrated into the core :)

  29. Amarjeet: How would that be possible, considering this tutorial was written before FF3? We can’t go back and test every tutorial for every new browser.

    Stephen: Thanks Re missing quote: Fixed.

    Stephen, ysamjo: Thanks. Re the Dimensions plugin: noted. Not sure how I, as Editor, can avoid such things, so it’s appreciated when a reader lets us know about updated libraries and plugins.

    For those of you saying “it doesn’t work for me,” it’d really help if you said which browser AND version. As I said in the comments above, I tested Jeff’s code for multiple new browsers (except FF3) and it worked fine. jQuery does not support some older browsers. Some simply render slightly incorrectly in terms of spacing or positioning.

    Matthew: Noted, but I’m at the mercy of what’s been submitted, ain’t I? :) jQuery is relevant right now, whereas database topics have to be within the frame of reference of popular CMSes. E.g., a tutorial on some sort of database manipulations for WordPress might appear, but a database tutorial not related to WP or another CMS/ blog platform likely will not.

  30. Cool effect, but I don’t think it’s very practical. I can’t think of a site that I could actually work it into its just to me a “flashy type effect.” I think it distracts from the site in general.

  31. I usually think these menus are annoying, but it always adds some life to the site that uses it. Thanks for the article.

  32. Could be useful for some upcoming projects. Thanks guys

  33. Very nice tutorial.

    I also have a similar tutorial which float the menus and banners in a smarter way:
    http://www.webresourcesdepot.com/smart-floating-banners/

    In this example menus are static but they float when needed and become fixed when needed again.

  34. Whats that font you use in the graphics?

  35. As a new designer, this website has helped me understand some of the basics involved for web development.

    Does anyone have any insight on the Alltop banner? I love it’s transparency and the home button.

    Any resources would be helpful.

    Thanks,
    Joseph

  36. Good tut! :)

  37. I just don’t care for that type of floating menu, maybe it’s just me, but they annoy me pretty much always.
    Nice tutorial though.

  38. Wouldn’t it be cleaner to put the “return false” for the links in the JS rather than placing it in an “onclick” on the A element?

    I agree with many others that this particular example is not a practical one or a good choice for a menu the tut itself is nice so thanks.

  39. Gravatar

    Junkie234

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

  40. Nice job! You should consider updating the example to not use dimensions since its now in the core.

  41. Very nice effect. Thanks very much. :)

  42. Gravatar

    tripdragon

    Ugh. Don’t teach people how to make these. These are gross. Just used fixed so it never moves and always stays in view.
    This is like watching a slug move on the screen.

  43. Doesnt work in firefox 3

  44. Don’t forget that NETTUTS tutorials are not just about the final effect but the coding techniques displayed. Whether or not you actually like floating menus or would ever use them, this tutorial demonstrates several jQuery techniques including animating elements. So the point is to absorb that and adapt the techniques to your own use.

  45. I have been interested in doing this for quite a while but have never bothered to look it up… Thanks for the tut!

    @Thomas Milburn - Yes you could just use position:fixed; but what would you do to accommodate IE6 users??

    btw Cannot believe people are still using inline JS!!! Hello! - What happened to unobtrusive beauty!??

  46. Very nice tutorial. It doesn’t matter that it doesn’t work right in firefox 3 beta 5 cause most people don’t have it. Maybe wait till they release it before you start complaining!

  47. @James
    Position:fixed can be made to work in IE6 using a few hacks. See this example on CSSplay http://www.cssplay.co.uk/layouts/fixed.html
    And you’re completely right about not using inline JavaScript. Same goes for inline CSS. Put it in an external file.

    BTW Has anyone noticed the rather strange formatting of the CSS code on the demo page? Have a look, it’s quite a good method!

  48. I find the effect itself a little distracting, maybe annoying for use on a website, but definitely a fun exercise. Look forward to getting into jQuery.

    Couple of questions: If you wanted such a menu without the jumpy-javascript effect would the menu work by making the position fixed and giving it a large z-index value?

  49. hmmm, I don’t think I would ever use it… it just seems “unprofessional”? or am I just old school?

  50. SUHHHWEEEETTTT!

    This is beautiful. Thank you.

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

  52. 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.!

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

  54. 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?

  55. wow very nice i will definitely use this =]

  56. easy code , but nice float menu ~~

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

  58. why does it work for your page???

  59. 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!

  60. nice one but needs improvement. Thanks

Add a Comment

Note: We use Gravatars on NETTUTS, they are little icons that appear next to your name on this site and on many others. You can get a Gravatar account for free and any other site that supports it will show your avatar too!

 

Trackbacks

Leave a Trackback