LavaLamp for jQuery lovers!

lavalamp-image

Click on the above image to land in the Lava Lamp Demo page. Then, hover over it and feel for yourself, the nifty effect that Lava Lamp offers. 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. Click on the image below to experience the demo for this underline-imageless lavalamp.

lavalamp-underline-image

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. Click on the image below to see the demo for this boxed-imageless lavalamp.

lavalamp-box-image

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.

Ganeshji Marwaha

I spend my days as the Director of Technology for Mobility practice and help my clients design enterprise and consumer mobile strategies. Mobile Payments, Digital Wallet and Tokenization technologies are my areas of specialization

  • Pingback: Los mejores tutoriales y recursos para hacer tus barras de navegación | SummArg()

  • This is wonderful platform that provides us an opportunity to [place our ideas successfully. I request all visitors to place ideas here that must be their own.

  • thnx for this menu…

  • ? wanna learn but ? didnt it. please help me please 🙁

  • nice post ,thank you for your sharing

  • thank you for your sharing

  • thank you

  • thanx

  • I believe this blog post is one of the most informative thing not only for the technique learner but also for necessary for all stage people…

  • thnkx

  • thnkxx you

  • Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

  • Decorating with a different gem accessory, shimmer, no minus than the form.To assemble the recent female’s liking, this reduction and coldn thomas sabo jewellery ess, largely to clothes and more to show the women’s boundary of the charm. In detail, many international brands of rage designers have i [url=http://www.thomassabouk.com/]thomas sabo jewellery[/url] ncisive out that the objects on each shape studs sequence. Series includes four different themes of form thomas sabo jewellery

  • good share. thnks.

  • Thank u. Good job.

  • ??

    ???????????

  • Great post. i enjoyed reading your blog. It was very interesting and informative.

  • That is a pretty interesting post. Thanks for the info. such a very great post.

  • Thanks to Google I was able to find this post, keep on posting high quality blogs like this information.

  • This website provides such a valuable information. Thanks

  • I was very encouraged to find this site. I wanted to thank you for this informative and useful read.

  • I believe this blog post is one of the most informative thing not only for the technique learner but also for necessary for all stage people…

  • Pingback: ?????240??jQuery?? - ?JAVA–???? java ???? ??????? ????()

  • Pingback: Fancy menú CSS3 » Letmon.com()

  • 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_?

  • Pingback: Different style de menu grace a Lavalamp()

  • Pingback: 240??jQuery?? | Wang Jun's Blog()

  • Pingback: CrazyLee » 240??jQuey??()

  • Pingback: TheBlog4.Biz Of | Blog | 40 New JavaScript Tutorials with Helping Techniques()

  • Pingback: jquery ???? | ???-?????()

  • Nice tutorial… thanks for sharing
    here an another jQuery menu link:

    http://www.bijusubhash.com/blog/redbrown-drop-down-menu-using-jquery-and-css

  • Danke Very Good

  • Excellent tool!

  • seems delicious like my cupcakes

  • Very nice this blog =)

  • This article gives the light in which we can observe the reality This is a really good read for me. its really very good post. Thanks for posting this informative article.

  • AK

    I need zep raw file

  • Pingback: Lava Lite 5225 16.3-Inch Silver Base Lava Lamp, Yellow/Purple | The Home Garden Stores()

  • ???

    Thanks so much for sharing this.
    Congratulation to all.

  • Lauder

    I feel so bad for the young kids who are getting into drugs and ruining there lives. I think kids now days feel so pressured into doing drugs, then they get hooked on them and can’t stop taking them. It really is sad.
    CK Perfume
    Kenzo Perfume
    Fendi Perfume

  • Sally

    Thank you very nice!
    Does anyone know how to make the lava effect to disappear when the menu is not being used?
    So when you hover over the menu THEN the lava effect appears.

    Thanks in advance 🙂

  • very nice thank you

  • julian

    when i use lightbox n conjunction with this script , the lavalamp menu turns doesnt work ..
    what could it be ?

  • Pingback: [?]??20??????jQuery???? | ????()

  • Hi,
    First I would like to congratulate you on making one of the best jquery nav plugins around.

    I wonder if I might be able to ask a question with regards to enlarging distance between links, and realligning the .backli(lava) in correspondence.

    Here is an explanation:

    I’ve just finished a rebuild of my site and have integrated lavalamp.

    In order to insert the lavalamp link bar I was forced to edit the margin in ‘.lavaLampWithImage li a’ from 10 to 31 in order to widen the link space in between one another.

    This made the floating hover graphic, in this case, the “/lava/lava.gif” extend to the right, still in line with the left side of the link, but extended to the point of almost reaching the second link, and the second link the 3rd, etc..

    So to compensate for this, I adjusted the margin right in the ‘.lavaLampWithImage li.back {‘ section from 7 to 30. This brought it back to centre on the current hover or current link, but has flattened it on the respective right side.

    Here is the site so you can see: http://www.nigeldunkley.com

    and the CSS below:

    Hopefully, someones got a clue, its been chomping at me for the whole day…

    Cheers, Happy New Year

    .lavaLampWithImage {
    position: absolute;
    height: 29px;
    width: 968.5px;
    background:url("../images/lava/bg.gif") no-repeat top;
    padding: 15px;
    margin: 10px 0;
    overflow: hidden;
    background-image: url(../images/lava/bg.gif);
    }
    .lavaLampWithImage li {
    float: left;
    list-style: none;
    }
    .lavaLampWithImage li.back {
    background:url("../images/lava/bg.gif") no-repeat right -30;
    width: 9px; height: 30px;
    z-index: 8; margin-left:15px;
    position: absolute;
    }
    .lavaLampWithImage li.back .left {
    background:url("../images/lava/lava.gif"); no-repeat top left;
    height: 30px;
    margin-right: 30px; /* 7px is the width of the rounded shape */ ---/* CHANGED FROM 7 TO 30) */
    }
    .lavaLampWithImage li a {
    font: 14px "Courier New", Courier, monospace;
    text-decoration: none;
    color: #fff;
    outline: none;
    text-align: center;
    top: 7px;
    letter-spacing: 20;
    z-index: 30;
    display: block;
    float: left;
    height: 30px;
    position: relative;
    overflow: hidden;
    margin: auto 31px; /* CHANGED FROM 10 TO 31) */
    }
    .lavaLampWithImage li a:hover, .lavaLampWithImage li a:active, .lavaLampWithImage li a:visited {
    border: none;
    }

  • Pingback: ?????240??jQuery?? | ??IT????()

  • Pingback: Pepsi Throwback & Lava Lamp | Electric Fondue()

  • Pingback: 17 Great Online Resources for Learning About JavaScript | denbagus blog()

  • Great to see such a nice article like this. Good information is posted in this post.

  • I really vary happy to visit this site as its give me enough knowledge…………

    Thanks!!!!