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,425 Responses to “LavaLamp for jQuery lovers!”

« 119 20 21 22 23 24 25 26 27 28 29 » Show All

  1. 1301. Salman on June 22nd, 2010 at 1:21 pm

    I used lavalamp , but when I try to use a link instead of “#” in anchors, it does not work,
    I have to right click on the text and choose “open link” to open it

  2. 1303. jQuery LavaLamp script | The Largest Forum Archive on June 23rd, 2010 at 8:02 pm

    [...] vb.net | No Comments » Hi Iam very begginer I want to use the script named lavaLamp from here http://www.gmarwaha.com/blog/2007/08…lovers/?cp=all ) but I donn’t know how I can use it I try to change the <li [...]

  3. 1304. deano on June 24th, 2010 at 10:07 pm

    HOW TO MAKE VERTICAL (BODGED!)

    very new to jquery, dont understand how the code works yet, but had a little trouble trying to get this nav working verticaly. the other tutorials out their dont have the same effect as this one thus i tried to modify the code from this tut as it was closest effect to what i wanted.

    until someone posts a clean solution to setup verticaly here are some very trial and error modifcations you can do with lavalamp.js

    $(this).hover(noop,function(){move(curr)});
    delete this line to stop the lamp returning to its current location

    return o.click.apply(this,[e,this])
    delete this to let links work

    reduce width of .lavaLampWithImage and the LI’s will fall below one another. add a background colour (to see) and increase height.

    this is not clean, i dont know how to code jscript, so backup before try, worked for me. ive just noticed that theirs a non-crunched (non-min) version of the code *DOH!*, wish i had seen that earlier. note that a straight up search for the above code might not work due to spacing but its in their.

  4. 1305. video izle on June 25th, 2010 at 9:21 am

    reduce width of .lavaLampWithImage and the LI’s will fall below one another. add a background colour and increase height.

  5. 1306. Jon on June 25th, 2010 at 10:36 pm

    Hey if you are atill confused about the tag being disabled, there is a solution:-
    click: function(event, menuItem) {
    return false;
    }

    change false to true and then it should work.

  6. 1307. seslichat on June 26th, 2010 at 12:08 pm

    I used lavalamp , but when I try to use a link instead of thanks admin

  7. [...] LavaLamp [...]

  8. 1309. Trip advice on June 28th, 2010 at 10:05 am

    I tried a lot of things, so there may have been something else I did as well, but I believe that this was the solution to lavalamp + superfish.

  9. 1310. joe on June 28th, 2010 at 7:28 pm

    I love this menu. I’m using it in my site and it looks great but I cannot get it to link to my other pages once the button’s are clicked. Could some one please help me with this?

  10. 1311. karakral on June 29th, 2010 at 4:05 pm

    I used lavalamp , but when I try to use a link instead of thanks

  11. 1312. Fancy menú CSS3- Noticias del Cerebro Digital on June 29th, 2010 at 8:52 pm

    [...] conocimos una versión utilizando jQuery, llamado lavalamp, el cual tambien se hizo popular por Ganesh . Esta vez, veremos cómo obtener el mismo efecto utilizando las nuevas características de CSS3. [...]

  12. 1313. serdar on June 30th, 2010 at 2:10 pm

    thank’s

  13. 1314. ??240??jQuery?? | ???? .NET,C#,??,???,????,??? on July 1st, 2010 at 12:56 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 [...]

  14. 1315. Yan Cui on July 2nd, 2010 at 12:37 am

    Thanks a lot!
    all working well!

  15. 1316. Green Web Hosting on July 2nd, 2010 at 12:29 pm

    Hey…..nice post!!

    Awesome, No more words to explain :) :) :D just….cool blog.

  16. 1317. Green Web Hosting on July 2nd, 2010 at 11:01 pm

    I would like to say “wow” what a inspiring post. This is really great. Keep doing what you’re doing!!

  17. 1318. Adam Olsen on July 2nd, 2010 at 11:03 pm

    Slick plugin, thanks! Definitely like riding elephants.

    Another solution to setting class=current for the right li when not using AJAX:

    $(document).ready( function(){
    $(‘.lavalamp [href='+window.location.pathname+']‘).parent().addClass(“current”);
    $(function() { $(‘#nav’).lavaLamp({ fx: ‘backout’, speed: 700 })});
    });

  18. 1319. Adam Olsen on July 2nd, 2010 at 11:04 pm

    Slick plugin, thanks! Definitely like riding elephants.

    Another solution to setting class=current for the right li when not using AJAX:

    $(document).ready( function(){
    $(‘.lavalamp [href='+window.location.pathname+']‘).parent().addClass(“current”);
    $(function() { $(‘.lavalamp’).lavaLamp({ fx: ‘backout’, speed: 700 })});
    });

  19. 1320. RubensaiD on July 3rd, 2010 at 6:08 am

    The correct solution is:

    $(function() {
    var direc = location.href;
    var bus;
    $(“.lavalamp li a”).each(function(i) {
    bus = this.href;
    if(bus == direc) {
    $(“.lavalamp li:eq(“+i+”)”).addClass(“current”);
    }
    })
    });

    that is before tha call to initialize the menu

  20. 1321. cadas18 on July 4th, 2010 at 1:27 pm

    Thanks You Man ! :)

  21. 1322. En iyi 240 adet JQuery Uygulamas? « ABDULLA TURHAN on July 4th, 2010 at 6:05 pm

    [...] Lava Lamp jQuery Menu. [...]

  22. [...] Read this tutorial >> [...]

  23. 1324. Naresh on July 7th, 2010 at 6:08 am

    cool…! effect. its awesome.. :)

  24. 1325. kompresör on July 7th, 2010 at 3:48 pm

    thank’s
    great..

  25. 1326. Brian on July 7th, 2010 at 11:10 pm

    Hey thanks for the amazing plugin Ganeshji! I’m having a small problem though, that perhaps you or someone else would be gracious enough to help me with.

    The background image on my menu is being cut off on the edges as it reshapes to the current link area. Is there any way to turn this off? I would like the image to bleed outside of each link area. You can see what I mean here: http://www.omni-flux.com/projects/valleytradesinc/Slider/slider.html

    Any help would be greatly appreciated. Thanks in advance!

  26. 1327. hal? y?kama on July 8th, 2010 at 6:51 am

    thank you bro

  27. 1328. sunucu kiralama on July 8th, 2010 at 6:52 am

    Thanks.

  28. [...] Web Site Download Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]

  29. 1330. Liste de 240 plugins Jquery | Worm's Blog on July 8th, 2010 at 1:53 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 [...]

  30. 1333. natural penis enlargement on July 11th, 2010 at 6:26 am

    Hi,Thanks for leading me to your Blog from a comment left on my Blog, some great stuff here, will bookmark it, Thanks again!

  31. 1334. 7 ???????? jQuery ????. | ????? ? ??????? ?? ???? jQuery. on July 12th, 2010 at 5:43 am

    [...] 2. jQuery ???? LavaLamp (????) [...]

  32. 1335. Harpreet Singh on July 12th, 2010 at 4:35 pm

    Links in menu not working. Even the demo(both versions) downloaded not working.

  33. 1336. Harpreet Singh on July 12th, 2010 at 4:40 pm

    Sorry about this. Links are working now the demo was returning false for click. remove the return false now working.

  34. 1337. Bernhard on July 13th, 2010 at 2:17 pm

    Thanks.
    Works cool!

  35. 1338. Celebs on July 14th, 2010 at 7:37 am

    In html you must edit javascript code for active links.

    Simply:

    $(function() {
    $(“#1, #2, #3″).lavaLamp({
    fx: “backout”,
    speed: 700,
    click: function(event, menuItem) {
    return true;
    }
    });
    });

  36. 1339. Malowanie on July 15th, 2010 at 12:25 pm

    Owesome menus! :) ) Thanks for this tut. Keep up the good work!

  37. 1340. Joël Kuiper on July 15th, 2010 at 11:44 pm

    In a case of “why on earth would you want to do that”: if you ever want to set the current after the initialization of the lamp you can add a little bind to the code:

    $(this).bind(“move”, function(e, el) {
    setCurr(el);
    });

    you can move by calling with $(“.lavaLamp”).trigger(“move”, v); with .lavaLamp being whatever element your lamp is and v the jqeury li object you wish to set the cursor to.

  38. 1341. Prabodha on July 16th, 2010 at 6:29 pm

    wow nice post and nice article.so beautiful..

  39. 1342. seguridad medicina laboral on July 17th, 2010 at 1:09 am

    i do not like the dots in the ul, li structure.

  40. 1343. Gideon on July 17th, 2010 at 11:01 pm

    This plug in does not work for blogger? I have tried everything I know of and cant get it to work. errors every time. can I not embed Java strait into blogger?

  41. [...] Nav Effect. This little bugger gave me trouble for some time! I originally found this jQuery LavaLamp effect for top navigation and thought it was pretty sweet. But, of course—I needed it styled for my [...]

  42. [...] Menu « Lavalamp » design : http://www.gmarwaha.com/blog/2007/08/23/lavalamp-for-jquery-lovers/ – Gestionnaire d’onglets : http://stilbuero.de/jquery/tabs_3/ – Menu accordéon simple : [...]

  43. 1346. Top 10 jQuery Plugins | Brio Daily on July 23rd, 2010 at 12:12 am

    [...] 7. LavaLamp jQuery [...]

  44. 1347. nandhini on July 23rd, 2010 at 10:14 am

    hello pls.have done a grt work…also i need the coding for…tats the lavalamp should hide… only on when mouse on menu the lava lamp should come..until then it should get highlighted

  45. 1348. 37 Amazing Tutorials for jQuery Navigation Menus on July 23rd, 2010 at 9:21 pm

    [...] 22-LavaLamp for jQuery lovers! [...]

  46. 1349. Danny on July 24th, 2010 at 12:18 am

    Hey that’s a nice effect you got there. I am still getting my head around jquery, and been doing some research.

    Keep up the good work.

    CHEERS :)

  47. 1350. Fabio on July 25th, 2010 at 5:19 pm

    Initially, it worked. It was just change “false” to “true” and everything worked. Thanks.

« 119 20 21 22 23 24 25 26 27 28 29 » Show All

Leave a Reply