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

  • cryx

    Sorry, it was wrong path in the .info file for Drupal, embarrasing. 🙂

  • forexborsa.net you bad hack site.

  • Pingback: Baller Living Rooms Need Fish Tanks: Tips | Plastic Fish Tanks()

  • cryx

    I have another problem. I can’t seem to get a “hover”-effect with the change of text color to the menu item, I don’t know why this doesn’t work!

    a:hover doesn’t do it.

  • it’s very cool,
    but with a rolling menu ?
    what do you thing we could have?

  • Thanks for the nice effect with LavaLamp for jQuery lover.

    Best regards
    Fusspflege

  • it’s very cool. i like it very much 😉

  • Pingback: LavaLamp un menu animé via jQuery | Creation site Intenet et logiciel, web agency Lyon – Blog AVANIM()

  • Latest version of Mozilla Firefox: The latest version of Mozilla Firefox is 3.6.6. In addition, it is considered as the most stable version of Firefox. It is powered by Gecko engine and the browser supports 75 different languages.

  • vinod

    Its very strange nobody give the exception when you use jquery core js library and jquery.easing.min.js. something conflicting is there when i am using jquery accordion and lavalamp menu. please do help.

  • vinod

    I need you zipped js files according to jquery1.8, please provide me the latest supported lavalamp.

  • Vignesh

    Fantastico….!!! 🙂

  • Okay a great deal I have on had the automobile for with regards to three weeks Now. this wasn’t until a week later when I was driving The auto by night, I noticed The the headlights of The automobile and also including The interior lights were dimming slightly and also obtaining brighter again (each two seconds The lights may dim, rather Next go rear To normal) at The very same time, I as well heard a faint whining noise which seemed To Follow The very same system as The lights (off and in, not continuous). the next morning I drove the car, as well as every via The headlights off, the whining continued. I as well checked went it was later from the AC, but which i has never this. it didn’t always come about each time turned the auto at. it seemed like it may occur in regards to ten minutes after I started driving this. also, the headlights only flickered when the engine was by use of. so I decided To take this To The dealership. They said it was the alternator, just so they replaced this as $700. Four days passed after they Fixed this and also The auto was OK. this wasn’t until yesterday I noticed the same thing most during again. can someone Tell me What is all wrong? (the car is to great illness otherwise via only 50,000 miles in this).

  • Luis

    Hi, is there a way to change the fx so that the hover background fades in and out instead of the default linear and backout effects?

  • Gordon Feinberg

    Thanks! Great job. Works really well. Much appreciated.

  • Pingback: Styling HTML Lists with CSS: Techniques and Resources | Ricardo JV Cruz()

  • Fanatik Spor Haberleri

  • i never thought that is very easy like how you discripe great work

  • this is a real tutorial thanks

  • i have a problem ending this i got nothing at the end

  • One of the important differences between a blog site in addition to a static websites would be the way people are capable of interact together with your articles. Having a static internet site it is just men and women studying phrases on a web page. After you possess a blog they are able to take a look at as well as remark back again! You ought to get that sort of interactive spirit heading so you can actually build a loyal readership with your website.

  • Pingback: Twitted by artysmedia()

  • It is a new line in my life and it is so awesome to find on the topic I want to deal for the rest of my life. Hopefully, I am the one that will be on one wave with you.

  • Nice menu. I have just the site for this. I love JQ. It’s so lightweight and as you say can rival flash for looks and applications.

  • This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

  • Follow the instructions on Guillermo Rauch’s page for the mootools version.

  • Pingback: LavaLamp Menu Plugin | Wordpress Plugins()

  • Great bit of code, thanks for sharing!

  • Pingback: LiveWebResources » Excellent jQuery Navigation Menu Tutorials()

  • Pingback: 240 plugin h?u ích c?a JQuery — XHVN.NET()

  • Pingback: 13??????Javascript?????? | Javascript???? – ????()

  • This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

  • Matthew

    Is it possible to make the menu be hidden, and when a button is clicked it slides up?

  • This article gives the light in which we can observe the reality. This is very nice one and gives in-depth information

  • Thanks for article

  • Great bit of code, thanks for sharing!

  • Great bit of code, thanks for sharing!

  • thanks you userfull article

  • thanks very good

  • thanks you userfull article

  • Helpful blog, bookmarked the website with hopes to read more!

  • Liseli kizlar liseli

  • David

    Am I missing something…. I replace a url Plant a tree where the # sign is and it doesn’t work. Help me understand please.

  • Pingback: 5 Simple & Useful Components for Portfolio Sites | Web Apps()

  • I admire your blog , it has of lot of information. You just got a perennial visitor of this site.

  • Hi, to start with I want to tell you that I follow your blog. Great post, I totally agree with you. Have a great day mate.

  • I recently came across your blog and have been reading along. I thought I would leave my first comment. I dont know what to say except that I have enjoyed

  • That was an awesome comeback

  • I admire your blog , it has of lot of information. You just got a perennial visitor of this site.