LavaLamp for jQuery lovers! August 23rd, 2007

Hover above and feel for yourself, the nifty effect of Lava Lamp. What you just experienced is nothing but the LavaLamp menu packaged as a plugin for the amazing jQuery javascript library. I personally believe that the effect rivals that of flash – Don’t you? Especially considering the fact that it is extremely light weight.

Just so you know, it weighs just 700 bytes(minified)!


Often I have noticed, that the credits are usually granted towards the end. Just for a change, i am going to give my credits at the beginning. This effect was originally written by Guillermo Rauch for the mootools javascript library. All I did was to port it for the benefit of jQuery lovers. Thanks Guillermo for inspiring the javascript world with such a nice effect. A special thanks to Stephan Beal who named it “LavaLamp”, and to Glen Lipka for generously helping with the image sprites. Many fellow jQuery lovers also helped shape this plugin with valuable feedback in the mailing list. Thanks a ton, all you guys.

As User Interface developers, we know that one of the first widgets our visitors use is a “Menu”. Capturing their attention right there is something that we always strive for, and I guess LavaLamp is a step in that direction. Before you get bored with all this useless talk, let me get you started on integrating LavaLamp into your jQuery powered site.

I hope you agree that a typical HTML widget consists of 3 distinct components.

  • A semantically correct HTML markup
  • A CSS to skin the markup
  • An unobstrusive javascript that gives it a purpose

Now lets follow the above steps and implement the LavaLamp menu for your site. Remember, In the process of porting from mootools to jQuery, i have simplified both the javascript and CSS for your convenience. So, be informed that you will need to follow the instructions on this page to get the jQuery version running. Follow the instructions on Guillermo Rauch’s page for the mootools version.

Step 1: The HTML

Since most UI developers believe that an unordered list(ul) represents the correct semantic structure for a Menu/Navbar, we will start by writing just that.

        <ul class="lavaLamp">
            <li><a href="#">Home</a></li>
            <li><a href="#">Plant a tree</a></li>
            <li><a href="#">Travel</a></li>
            <li><a href="#">Ride an elephant</a></li>
        </ul>
        

In the markup above, “ul” represents the menu, and each “li” represents a menu-item. At this point it is crucial to understand that we will be adding another artificial “li” to represent the background of the currently highlighted menu-item. Since the background itself is cosmetic and doesn’t represent a menu-item, we will be adding it from javascript. Just to make sure we are in sync, “you need not add this li”, the LavaLamp plugin will take care of it. Once added, the “li” representing the background will look like this.

        <li class="back"><div class="left"></div></li>
        

Step 2: The CSS

You can skin this markup in many different ways to achieve your own personalized menu. The following style sheet is just one possibility. A few more possibilities are demonstrated in the “Bonus” section towards the end of this blog entry.

/* Styles for the entire LavaLamp menu */
.lavaLamp {
    position: relative;
    height: 29px; width: 421px;
    background: url("../image/bg.gif") no-repeat top;
    padding: 15px; margin: 10px 0;
    overflow: hidden;
}
    /* Force the list to flow horizontally */
    .lavaLamp li {
        float: left;
        list-style: none;
    }
        /* Represents the background of the highlighted menu-item. */
        .lavaLamp li.back {
            background: url("../image/lava.gif") no-repeat right -30px;
            width: 9px; height: 30px;
            z-index: 8;
            position: absolute;
        }
            .lavaLamp li.back .left {
                background: url("../image/lava.gif") no-repeat top left;
                height: 30px;
                margin-right: 9px;
            }
        /* Styles for each menu-item. */
        .lavaLamp li a {
            position: relative; overflow: hidden;
            text-decoration: none;
            text-transform: uppercase;
            font: bold 14px arial;
            color: #fff; outline: none;
            text-align: center;
            height: 30px; top: 7px;
            z-index: 10; letter-spacing: 0;
            float: left; display: block;
            margin: auto 10px;
        }
        

Trust me, this is a simple style sheet. Follow along to understand what is done in each of its sections.

First, we style the “ul” with the bright orange background image and some basic properties like height, width, padding, margin etc. We use relative positioning because, that way we can absolutely position the background “li” relative to the “ul”. This helps by enabling us to move this background “li” freely within the context of the parent “ul”.

Next, we make the “li”s flow horizontally instead of vertically. By default, it flows vertically. There are a couple of techniques to do this. In this case, we are using the “float:left” to achieve this effect.

Next, we style the artifical “li” that represents the background of the currently highlighted menu-item. This uses the sliding doors technique. Also, notice the absolute positioning used as mentioned above.

Finally, we style the anchor that represents the actual clickable portion of each menu-item. These styles are mostly cosmetic and self-explanatory.

Some of the above rules may not be obvious if you are not very confident in how “positioning” works in CSS. For those, i highly encourage you to quickly read this article on CSS positioning. It is short, sweet and very informative.

Step 3: The Javascript

This is the easy part. Most of the javascript work is taken care by the Lava Lamp plugin itself. As a developer, you just have to include the mandatory and/or optional javascript files and fire a call to initialize the menu.

<script type="text/javascript" src="path/to/jquery.js"></script>
<script type="text/javascript" src="path/to/jquery.lavalamp.js"></script>
<!-- Optional -->
<script type="text/javascript" src="path/to/jquery.easing.js"></script>

<script type="text/javascript">
    $(function() { $(".lavaLamp").lavaLamp({ fx: "backout", speed: 700 })});
</script>
        

Include a reference to the jQuery library and the LavaLamp plugin. Optionally, include the easing plugin as well. It has many cool effects, that are not contained in the core. For instance, the “backout” effect used in this demo is part of the easing plugin. You can download jQuery here, Easing plugin here, and the LavaLamp plugin here.

Next, in the document.ready event, fire a call to initialize the menu. You have the option to supply an easing “fx” , the “speed” with which the animation happens and a callback to be executed when a menu-item is clicked. They are optional, the default “fx” being “linear” and the default “speed” being “500″ ms.

That’s it. At this point you should have a working version of LavaLamp menu for your site.

Bonus

Just with some minor changes in the style sheet, you can get a totally different look n feel for the menu. And yes, the HTML markup and the Javascript remain the same.

Here is one more variation, again with just some minor changes to the style sheet. I know, they don’t look pretty, but all i am saying is that you are limited only by your imagination.

Finally, for your convenience, i have zipped up all the necessary files into a cohesive package. Download it, and open the demo.html to see all the 3 variations in one page.

Feel free to leave a comment with your feedback, suggestions, requests etc.

Update

Based on popular request, LavaLamp Menu has been updated to support jquery 1.2.x versions. Download the zip file for version 0.2.0 of LavaLamp and open the demo.html to check it out for yourself. Since Firefox 3 has some issues with $(document).ready() function, try using $(window).load() instead if you face any problems. Hopefully a future version of Firefox or jQuery will fix the problem.

1,432 Responses to “LavaLamp for jQuery lovers!”

« 117 18 19 20 21 22 23 24 25 26 2729 » Show All

  1. 1051. Jquery ile haz?rlanm?? men?ler - Webmaster Forumu on February 1st, 2010 at 2:15 am

    [...] [...]

  2. 1052. Kamlesh wagh on February 1st, 2010 at 9:02 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  3. 1053. Kamlesh wagh on February 1st, 2010 at 9:02 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  4. 1054. Kamlesh wagh on February 1st, 2010 at 9:03 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  5. 1055. Kamlesh wagh on February 1st, 2010 at 9:03 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  6. 1056. Kamlesh wagh on February 1st, 2010 at 9:03 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  7. 1057. Kamlesh R. wagh on February 1st, 2010 at 9:26 am

    hi friend,

    i am using lavalamp menu in my project, i am giving links to menus but when i am go to “about us” page from “home” page the menu selection box is appearing on “home” link, plz gaid me how can i appear that on “about us” menu in “about us” page.

    thanks in advance
    plz reply…..

    Regards,
    Kamlesh Wagh
    Nashik, Maharashtra
    India

  8. 1058. Jose Duenas on February 1st, 2010 at 4:48 pm

    If you are using Lavalamp menu in WordPress and you want to highligth the current page item, you just have to repalce all ocurrencies for “current” in jquery.lavalamp.js to “current_page_item”.

  9. 1059. Junnel on February 4th, 2010 at 3:29 am

    May i know how to put a dropdown submenu in my main menu that will ease when i hover it.Please help. thanks…

  10. 1061. RedKoala on February 5th, 2010 at 8:19 am

    Hey Ganesh
    Big thanks for this Plugin – Lavalamp looks awesome!
    I put it on my website http://www.redkoaladesign.pl

  11. 1062. Top 15 Jquery CSS Animated Navigation Tutorials – Designzzz on February 5th, 2010 at 6:32 pm

    [...] Demo | Tutorial [...]

  12. 1063. Theo on February 5th, 2010 at 9:55 pm

    Maybe a stupid question ? would this work on a vertical menu ?

    Thank you for sharing !

  13. 1064. Spencer on February 6th, 2010 at 2:28 am

    Hey,

    I am wondering the same thing that others on this page have asked about keeping the current page highlighted when you click the link. If anyone has a solution I would really appreciate it. Thanks

    P.S You need a better spam filter!

  14. 1065. chris on February 6th, 2010 at 6:11 pm

    hey thank you for this javascript it help me to release my new website
    you are so great here i have used it http://www.khoatu.de/

  15. 1066. Creating a Fancy menu using CSS3 and jQuery | INSIC DESIGNS on February 8th, 2010 at 6:49 pm

    [...] the Mootools library. And later a jQuery version of this menu called lavalamp was made popular by Ganesh. This time I will show you how to achieve the same effect using the CSS3 new [...]

  16. 1067. Raffaell on February 8th, 2010 at 7:27 pm

    Hi…

    I’m using ur scripts as a normal scripts (not for wp or joomla).
    Now the problem is how to fix the .current issue ?
    Everytime i click the links, the position is back to the first li.

    Too many comments already and make me dizzy to find the solution.

    Thanks,
    Raff

  17. 1068. 20 Cool jQuery Tricks for Web Developers | DSpot Inc on February 10th, 2010 at 7:55 am

    [...] 11.) Build a LavaLamp Menu [...]

  18. 1069. Things I Found Interesting Around February 9th | Chris Coyier on February 10th, 2010 at 3:52 pm

    [...] LavaLampI did a thing just like this on CSS-Tricks, this is the original [...]

  19. 1070. Rawler on February 10th, 2010 at 7:16 pm

    is there a way to enable/disable menu sub-items in this control?

  20. 1071. Erum on February 12th, 2010 at 9:34 am

    Love the plugin!!! Thanks!!!!

    Is there a way to set the default menu item?

    For example the selection stays put on Home. If I’m on the About us page, I’d like it to stay/start from the about us link.

    Is there a setting which will achieve this?

    Thanks!

  21. 1072. Prateek on February 12th, 2010 at 7:50 pm

    Hey,
    Can you please explain your reply to comment #732.
    I am a newbie to all this jQuery and lavalamp. What I want is what John wants. I want lavalamp menu to be able to control tabs (be it coda slider or anything else).
    Please help me on how to do it.
    Also,
    I noticed that in your example if I increase number of characters in a menu item say “Riding an Elephant11111111111″ then after a fixed number of characters the lava.gif image that hovers BREAKS

  22. 1073. Prateek on February 12th, 2010 at 8:09 pm

    I want something like this http://sinanyasar.com/blog/?p=80
    But just that I don’t understand the language this blog is written in and Google Translate isn’t giving any meaningful results

  23. 1074. behi on February 12th, 2010 at 9:30 pm

    i want change default blue line from “home” to “Ride an elephant”.
    what i do??????

  24. 1075. behi on February 12th, 2010 at 9:59 pm

    therefore, i will changed this menu work from left to right state, because “Home” in my language is at right side and “Ride an elephant” is at Left side.
    please help me…

  25. 1076. egypt web designer on February 12th, 2010 at 11:59 pm

    wow, I tested it, and it work this is the first time to use a jquery menu

    thanks :-)

  26. 1077. Danial on February 14th, 2010 at 1:07 am

    I’ve used it, its really good but i can’t use it with CSS Dock Menu Jquery together in the same website. i don’t know why but I’ve really tried it, does any one know how to fix it? let me know please. danial_comp@yahoo.com

  27. 1078. Catherine Miller on February 14th, 2010 at 4:12 pm

    Your script is great and I love it, but I’m having trouble when I am using a custom font for the menu (@font-face). It works swimmingly in IE and Firefox on a Mac, but in Firefox 3.5 on a PC, the “current” hover state displays incorrectly until the menu is rolled over. I believe it is loading the default font first, placing the hover in the space where it thinks it belongs, then the TTF font finally loads and shifts the menu, so now the hover is in the wrong spot. I’ve tried putting in FOUT scripts, but they have the same result (even though a FOUT script will hide the default font from the browser until the custom font is loaded, the loading order problem is still the same). Is there anyway to delay the loading of the “li.back” class until after the custom font has loaded, or a way to redraw the page so that it knows that the text has changed? I’m tearing my hair out trying to make this work with the custom font before I give up and go back to a web font. Thank you for any help you can provide me!

  28. 1079. Tushar on February 15th, 2010 at 6:41 pm

    thanx friends this is so amazing for my site
    Thanks Again lot………
    This will help me lot

  29. 1080. Erum on February 16th, 2010 at 7:40 am

    If I try a link like this is in the menu:
    a href=#something
    it does not work if I’m using this plugin.

    Any workaround will be appreciated.

  30. 1081. Erum on February 16th, 2010 at 7:49 am

    On second thoughts, none of the links work.

    I thought may be I had changed something. So I downloaded the plugin again and just added a link in your menu and it is not working.

    Nothing happens when I click on the menu link.

  31. 1082. Fitiwizz on February 16th, 2010 at 9:05 am

    Hi, very nice plugin ;)

    I want to set the current object by clicking on another link in the page than the lavalamp link himself. How can I do that ?

    (i’m french, so sorry for my bad english xD)

    Thank’s you ;)

  32. 1083. Roshan on February 16th, 2010 at 8:39 pm

    My links doesn’t works in your lavalamp menu. I inserted at but also not working. Is there another way?

  33. 1084. timtam on February 16th, 2010 at 9:12 pm

    As per previous comments – It looks great but does not seem to hyperlink to other pages ?. I have tried it in 4 different browsers. Am i doing something wrong ?

  34. 1085. ???? on February 17th, 2010 at 5:04 am

    My links doesn’t works in your laval

  35. [...] Lava Lamp Menu Attached Files [...]

  36. 1087. John on February 18th, 2010 at 10:26 am

    Same problem as many others – everything works fine except the actual hyperlinks do not work when clicked. however, right clicking and selecting ‘open in new tab’ etc, they do work. Any suggestions?

  37. 1088. Leo on February 18th, 2010 at 10:42 am

    @John

    If you remove

    “,
    click: function(event, menuItem) {
    return false;
    }

    From the javascript the links will work, note to remove the comma after the speed value as well.

  38. 1089. John on February 18th, 2010 at 12:43 pm

    @Leo

    Thank you so much, works perfectly now!

  39. 1090. Macrostone Blog » Blog Archive » on February 18th, 2010 at 4:35 pm

    [...] jQuery SuckerFish Style. jQuery Plugin Treeview. treeView Basic. FastFind Menu. Sliding Menu. Lava Lamp jQuery Menu. jQuery iconDock. jVariations Control Panel. ContextMenu plugin. clickMenu. CSS Dock Menu. jQuery [...]

  40. 1091. Jquery Kütüphanesi « Birebir Payla??m on February 18th, 2010 at 9:25 pm

    [...] for jQuery jQuery SuckerFish Style jQuery Plugin Treeview treeView Basic FastFind Menu Sliding Menu Lava Lamp jQuery Menu jQuery iconDock jVariations Control Panel ContextMenu plugin clickMenu CSS Dock Menu jQuery Pop-up [...]

  41. [...] 11.) Build a LavaLamp Menu [...]

  42. 1093. m3 ds real on February 19th, 2010 at 12:24 pm

    Nice tips for HTML and Java!thanks dude…from read other’s problems, I got my answer!Thanks for this information.

  43. 1094. Christian on February 19th, 2010 at 1:52 pm

    Is it possible to control Lavalamp with a second menu, too?
    I’ve got a Lavalamp menu on top of the website and a second menu in a sidebar, which conains the same entries. I want to update the curr variable with the value of the item I clicked in the side menu, but I don’t know how to do it.

    Thanks!

  44. 1095. Christian on February 19th, 2010 at 2:17 pm

    … meanwhile I found it out myself :)

  45. 1096. ????jQuery?? « ?? on February 20th, 2010 at 7:18 am

    [...] jQuery SuckerFish Style. jQuery Plugin Treeview. treeView Basic. FastFind Menu. Sliding Menu. Lava Lamp jQuery Menu. jQuery iconDock. jVariations Control Panel. ContextMenu plugin. clickMenu. CSS Dock Menu. jQuery [...]

  46. 1097. ?????240??jQuery?? « theU0net.Blog on February 22nd, 2010 at 1:24 am

    [...] jQuery SuckerFish Style. jQuery Plugin Treeview. treeView Basic. FastFind Menu. Sliding Menu. Lava Lamp jQuery Menu. jQuery iconDock. jVariations Control Panel. ContextMenu plugin. clickMenu. CSS Dock Menu. jQuery [...]

  47. 1098. BluechipJ on February 22nd, 2010 at 6:37 pm

    Is there any way to change the colours similar to MagicLine’s CSS trick, which unfortunately, doesn’t work with Internet Explorer.

    http://css-tricks.com/examples/MagicLine/

  48. 1099. larry on February 22nd, 2010 at 11:41 pm

    hi,

    How do you modify this to handle down down sub menu off the list tag?

  49. 1100. Tobias on February 23rd, 2010 at 1:25 pm

    Yes, this is what I’d like to know as well. At http://apycom.com/menus/1-white-smoke.html, they have multilevel menus, but AFAICS, the lavalamp effect (if present) spans the topmost level only.

« 117 18 19 20 21 22 23 24 25 26 2729 » Show All

Leave a Reply