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.

2,229 Responses to “LavaLamp for jQuery lovers!”

« 135 36 37 38 39 40 41 42 43 44 45 Show All

  1. 2201. Natalya on January 13th, 2012 at 9:24 pm

    Great effect, however this code has caused my menu links stop working. Any suggestions?

  2. 2202. jQuery?? | ??? on January 16th, 2012 at 3:53 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 [...]

  3. 2203. steve on January 18th, 2012 at 8:28 pm

    I too am having issues with my menu links. Now none of the links will work, how do I fix this?

  4. 2204. steve on January 18th, 2012 at 8:44 pm

    I saw in past posts that you need to change the click function from false to true in the java…

    click: function(event, menuItem) {
    return true;

    hope that helps others who are having issues with the links not working as well…

  5. 2205. EbowGB on January 20th, 2012 at 3:12 pm

    A fantastic plugin.

    Noticed that with jquery & jquery-ui included from the Google repositories, the following changes are needed:

    - don’t include the jquery.js from the download zip
    - don’t include the easing.js from the download zip
    - in the given example, above and in the download zip, change:

    fx: “backout”

    to

    fx: “easeOutBack”

    Newer version of the easing library changed the names.

    HTH.

  6. 2206. tercüme on January 20th, 2012 at 10:50 pm

    hey Ganesh. thank you so much. i”m using 2 of your plugins together- lavalamp and jcarousellite- and they r working beautifuly.
    i do have one question… how can i make LavaLamp work whit key navigation? i”m trying to add jquery keydown() event but whit no luck… is it even possible?

  7. 2207. Joseph Buarao on January 21st, 2012 at 11:27 am

    cool menu.. but I got a problem in IE7, can you help to fix?

  8. 2208. Xbox Live Account | Gaming Stuff on January 22nd, 2012 at 12:14 am

    [...] online gaming service on consoles that charges every user a certain fee for playing it. Tags: | the impossible game | the world game | skateboarding games | gaming news ← Online Gaming: The Rewards It [...]

  9. 2209. Shaik on January 22nd, 2012 at 1:15 pm

    Is it possible to have a submenu along with this lavalamp imlementation?
    The current implementation is always selecting the first page(Home menu item). How can I change the selection to the second or third menu items?

    I’m new to jQ. Sorry for being naive.

  10. 2210. top music on January 26th, 2012 at 7:00 am

    Wow… wonderful piece of an article.

  11. 2211. njoy on January 27th, 2012 at 4:03 pm

    shares, shares enjoyed a very good site

    I love the wonder

  12. 2212. David W. Allen on January 28th, 2012 at 2:47 am

    Gee thanks for the code. I have been looking for a working example of this type of menu as I want to apply something similar on my greeting card website. I have done some tests so far and all working fine. Thank you.

  13. 2213. webdesigner on January 28th, 2012 at 4:20 pm

    good job!thank you

  14. 2214. african mango reviews on January 29th, 2012 at 3:56 am

    I think without big “G”, all will be hell.

  15. [...] LavaLamp for jQuery lovers! [...]

  16. 2216. Jansen on January 29th, 2012 at 10:11 am

    When i implement the lavamenu on my site, the lavamenu works.
    But my other jquery plugins (for example a slider with a “onclick” function) does not work anymore.
    Does someone have a suggestion how what i can do?

  17. 2217. cloudyan on January 30th, 2012 at 5:15 am

    cool,It’s very good!

  18. [...] Demo | Source page [...]

  19. 2219. north face outlet on February 2nd, 2012 at 5:28 am

    Have you ever walked past a designer shop, looked in the window display the north face outlet and felt as though a bag was calling you by name? Or maybe you happen to see it in a catalogue? North face store That’s beautiful things and tell yourself apart. Ok, so now to make the tough cheap north face decision on why you should spend $1000 or more (can be several hundred thousands) on. Thank you for posting .please do more good article.the north face outlet it’s so cool .i like it very much.

  20. 2220. skip hire on February 2nd, 2012 at 6:17 am

    Wow this plugin would surely be very helpful in jQuery coding ad hence development will be much easier.

  21. 2221. su aritma on February 2nd, 2012 at 1:01 pm

    Great sharing.

  22. 2222. Risikolebensversicherung ohne Gesundheitsprüfung on February 2nd, 2012 at 2:04 pm

    The tutorial is very informative and innovative. I’m very glad to collect these useful info which is very useful for me.

  23. 2223. nancyjohn on February 2nd, 2012 at 2:15 pm

    Hey lava Lamp is such a good technology. I am loving it a lot.link building service

  24. 2224. mark_volan on February 2nd, 2012 at 4:51 pm

    A great attempt to bring out the content I must admit.
    Douglasville bankruptcy lawyers

    Hydroxatone

    Max Burn

  25. 2225. breast actives on February 3rd, 2012 at 12:45 am

    What does that mean by Jlover ?I really don’t understand !

  26. 2226. Cashew Dehuller for Sale on February 3rd, 2012 at 6:10 am

    Always bailing on work, gotta go help, have to stay home today, the pregnancy is high-risk, have to go to this appointment, that appointment…

  27. 2227. ????? » Blog Archive » ????????current?????? on February 3rd, 2012 at 7:12 am

    [...] ??????????????????????????????????????jQuery??????LAVALAMP?????????????????current????????????????????????????????? LAVALAMP [...]

  28. 2228. ionizer air purifier on February 3rd, 2012 at 2:55 pm

    Definitely i will try this tutorial and than give any opinion.

  29. 2229. tercüme on February 3rd, 2012 at 6:14 pm

    Great post. Thanks.

« 135 36 37 38 39 40 41 42 43 44 45 Show All

Leave a Reply