Create a Floating Menu with jQuery
Oct.07, 2009 in
Web Design & Development
Here is the code you can use to create a floating jQuery menu that stays where you absolutely position it on the screen. Make sure you have jQuery loaded on whatever page you use this. Try it out yourself!
HTML
<div id="floatMenu"> <h3>Floating Menu</h3> <ul> <li><a onclick="return false;" href="#">Menu Item 1</a></li> <li><a onclick="return false;" href="#">Menu Item 2</a></li> <li><a onclick="return false;" href="#">Menu Item 3</a></li> <li><a onclick="return false;" href="#">Menu Item 4</a></li> <li><a onclick="return false;" href="#">Menu Item 5</a></li> <li><a onclick="return false;" href="#">Menu Item 6</a></li> <li><a onclick="return false;" href="#">Menu Item 7</a></li> <li><a onclick="return false;" href="#">Menu Item 8</a></li> </ul> </div>
Javascript
<script language="javascript" type="text/javascript"> <!--// $(function(){ function moveFloatMenu() { var menuOffset = menuYloc.top + $(this).scrollTop() + "px"; $('#floatMenu').animate({top:menuOffset},{duration:500,queue:false}); } menuYloc = $('#floatMenu').offset(); $(window).scroll(moveFloatMenu); moveFloatMenu(); }); //--> </script>
CSS
#floatMenu { position:absolute; top:30%; left:10px; width:135px; background-color:#FFF; margin:0; padding:0; font-size:11px; border-left:1px solid #ddd; border-right:1px solid #ddd; } #floatMenu h3 { color:white; font-weight:bold; padding:3px; margin:0; background-color:#006; border-bottom:1px solid #ddd; border-top:1px solid #ddd; font-size:13px; text-align:center; } #floatMenu ul { margin:0; padding:0; list-style:none; } #floatMenu ul li { padding-left:10px; background-color:#f5f5f5; border-bottom:1px solid #ddd; border-top:1px solid #ddd; } #floatMenu ul li a { text-decoration:none; }


Hey, thanks for the simple script. How can I calculate an offset for the bottom so I can accommodate a footer? Thanks.
You’re welcome Al, thanks for the comment!
You can change the code to create a fixed-width centered horizontal floating menu 100px from the bottom of the page by updating these CSS sections:
and
I also noticed that the background does not show when you position using
bottomcss property, to counteract this i added a line to themoveFloatmenu()javascript function:Does this answer your question? If not please be more specific in your reply, thanks!
Josh,
Thanks for the quick reply. That doesn’t quite do it for me. While the bottom:100px works, it pushes the entire div upwards 100px. I have to fit the menu within a space on my right bar.
I’m trying to figure out how to add a bottom offset…
Josh,
Found another script that worked for me.
http://plugins.jquery.com/project/stickyfloat
Thanks for all your help, appreciate it!
Glad you found something that works, thanks for the link to that script, looks very useful.
Thanks for this small script.
having a bit of trouble with getting this into WordPress.
http://www.warpconduit.net/2009/10/07/create-a-floating-menu-with-jquery/
i’m putting the menu at the bottom of the footer template, just above
the javascript in the header template just above
not that sure about the css, so I assumed that went in the stylesheet, at the bottom?
Anyway, it doesn’t work.
Any advice?
@Tom:
This is the raw HTML, JS and CSS needed for the floating menu and will need to be adapted for WordPress. You first need to make sure you are loading jQuery, usually by using
wp_enqueue_script('jquery');in yourfunctions.phpfile and then change all the $’s in the JS code to jQuery because WordPress’s jQuery runs in “No Conflict” mode.Example:
$(window).scroll(moveFloatMenu);
becomesjQuery(window).scroll(moveFloatMenu);
You are correct in placing the CSS, or you can place it in a separate file and link the stylesheet to your page. The HTML can go anywhere on the page. The CSS positions the menu div and the JS will keep it there.
Thanks for the jquery fix for the background oddity when wanting to work from the bottom. It worked great for me!
[...] http://www.warpconduit.net/2009/10/07/create-a-floating-menu-with-jquery/ [...]
I really love to read articles that have good information and ideas to share to each reader. I hope to read more from you guys and continue that good work that is really inspiring to us.
it really good post. i have used it.