13. July 2009 by Janko in Tutorials | tags: ,

This tutorial will show you how to turn unordered list (UL) into an fancy social bookmarking sharing box. You will see how to style such box, how to add interactivity, and how to create jQuery plugin that will turn any UL into sharing box.

Download source View demo

The idea is to have all of the buttons shown inline and partially hidden. When user hovers over a button it slides to the right and become visible.

 

Styling unordered list

We will use unordered list to create a simple list that will show icons of popular social bookmarking sites. First list item won't be an icon but rather an image saying "share this". Icons used in this tutorial are from Function Icon Set.

<ul class="sharebox">
    <li><img src="sharethis.png" alt="Share this" /></li>
    <li><a href="#"><img src="digg_48.png" alt="Sumbit to Digg" /></a></li>
    <li><a href="#"><img src="sumbleupon_48.png" alt="Sumbit to StumbleUpon" /></a></li>
    <li><a href="#"><img src="delicious_48.png" alt="Sumbit to Delicious" /></a></li>
    <li><a href="#"><img src="technorati_48.png" alt="Sumbit to Technorati" /></a></li>
    <li><a href="#"><img src="reddit_48.png" alt="Sumbit to Reddit" /></a></li>
    <li><a href="#"><img src="mixx_48.png" alt="Sumbit to Mixx" /></a></li>
</ul>

We'll leave HTML anchors empty here so you have to replace them with real links. Now let's add some styles to make this list a little bit nicer.

ul.sharebox { margin:0px; padding:0px; list-style:none; }
ul.sharebox li { float:left; margin:0 0 0 0px; padding:0px; }
ul.sharebox li a { margin:0 0 0 -24px; display:block; }
ul.sharebox li a:hover { margin:0 0 0 -8px; }
ul.sharebox li img { border:none;}

Now the list looks fine, but this is not what we wanted to do. Right side of each button (except the last one) is hidden so they won't slide to the right. Each button should be shown under the previous one, and that is not the case here.

 

To carry out what we wanted from the beginning, we need to involve absolute inside relative positioning. Each list item will be absolutely positioned inside relatively positioned unordered list. This means that each button will appear at position top:0px and left:0px.

ul.sharebox { margin:0px; padding:0px; list-style:none; position:relative;}
ul.sharebox li { float:left; margin:0 0 0 0px; padding:0px; position:absolute; } 

Adding jQuery

Image above shows how buttons will appear when we make them absolutely positioned. What we have to do next is to involve some jQuery. To make each button appear under the previous one, we have to apply z-index CSS attribute properly and to move each button to its place.

$(document).ready(function(){
    var i = 10;
    var j = 0;
    $("ul#sharebox li").each(function() {
        $(this).css("z-index", i)
        if (j > 0)
            $(this).css("left", j * 24 + 100 + "px");
        i = i - 1;
        j = j + 1;
    });            
}); 

The code above iterates through collection of list items and applies z-index to each list item. Z-index starts with 10 and decrement by one for each list item. This means that each button will have z-index less than its predecessor and will appear under it. This is what we wanted from the beginning. This code also assigns values to "left" attribute for each list item. Each list item will be partially shown, exactly 24px to the right (half of total button width). Also, each button will be moved 100px to the right because that space is reserved for "share this" image. This is why we don't set "left" attribute to the first list item (j>0).

Creating plugin

This can be turned into plugin easily. Create sharebox.js file, and copy&paste jQuery function we created previously. Instead of "ul#sharebox li" selector, we'll use local variable "element" that refers to the element to which plugin is applied. We'll then select all list items using find("li") function. Everything else remains the same.

(function($){
    $.fn.sharebox = function(){
        var element = this;
        var i = 10;
        var j = 0;
        $(element).find("li").each(function(){
            $(this).css("z-index", i)
            if (j>0)
                $(this).css("left", j * 24 + 100 + "px");
            i = i - 1;
            j = j + 1;
        });
    }
})(jQuery); 

To apply plugin to some unordered list you just type this:

$("#list").sharebox();   

Where "list" is id of unordered list.

Sharebox is now fully functional. This code was tested in all major browsers, FF3, Chrome 2, Safari 4, IE 6, 7 & 8. If you find any bug or if you know some easier way to the do same, please share!

If you liked this article why don't you share it:

Create fancy share box with CSS and jQuery (via @jankowarpspeed) Share this on StumbleUpon Share this on delicious Share this on Digg Share this on Dzone Share this on DesignBump Send this to friend

Comments

Pingbacks and trackbacks

  1. Pingback from designm.ag Create fancy share box with CSS and jQuery
  2. Pingback from tablosign.com Iconos sociales despegables con CSS y jQuery — Tablosign
  3. Pingback from vhg-design.com Create fancy share box with CSS and jQuery « Freelance Web Designer
  4. Pingback from imaginaryworld.net Internet Brain » Create fancy share box with CSS and jQuery
  5. Trackback from etc. Create a Fancy Share Box ... and more!
  6. Pingback from kygeek.com Daily Links for Tuesday, July 14th, 2009
  7. Pingback from synergystars.co.cc Create a Fancy Share Box … and more! | Synergystars
  8. Pingback from jaceju.net 網站製作學習誌 » [Web] 連結分享
  9. Pingback from tripwiremagazine.com 60+ Must Have jQuery Toolbox | tripwire magazine
  10. Pingback from colordeu.es Caja creada con jQuery y Css de iconos de redes sociales para compartir contenido - colorate
  11. Trackback from You are now listed on FAQPAL Create fancy share box with CSS and jQuery
  12. Pingback from muliati.com 60+ Must Have jQuery Toolbox | The Muliati.Com goBlog
  13. Trackback from CREAMU ブログにかっこいいパーツをつけるチュートリアル10
  14. Pingback from smkn.xsrv.jp ITキヲスク | 2009年7/26~8/1の週間ブックマーク
  15. Pingback from guidesigner.net Best jQuery Plugins/Techniques For Web Designers And Developers | guidesigner.net
  16. Pingback from realtechsynergy.com 20 jQuery Plugins/Techniques » Synergy Computer and Network Services Private Limited
  17. Pingback from devblab.net 20 plugins / Técnicas jQuery Para diseñadores Web y desarrolladores | DevBlab
  18. Pingback from blog.4dpixelworld.com Best jQuery Plugins/Techniques For Web Designers And Developers | Web Design GroundBreak
  19. Pingback from labs.autobidlive.co.nz Create fancy share box with CSS and jQuery
  20. Pingback from tripwiremagazine.com 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets | tripwire magazine
  21. Pingback from blog.studiopyros.com.br JQuery & MooTools Tutorial Compilation | Pyros Blog
  22. Pingback from huibit05.com 95+ Exceptionally Useful jQuery Plugins, Tutorials and Cheat Sheets | huibit05.com
  23. Pingback from smashingmagazine.com The Z-Index CSS Property: A Comprehensive Look « Smashing Magazine
  24. Pingback from webolia.com The Z-Index CSS Property: A Comprehensive Look
  25. Pingback from locallab.wordpress.com The Z-Index CSS Property: A Comprehensive Look « LocalLab : Foire aux Infos
  26. Pingback from wpconfig.com Wordpress Blog Services - The Z-Index CSS Property: A Comprehensive Look
  27. Pingback from album.amb.com.tw AMB Album » The Z-Index CSS Property: A Comprehensive Look
  28. Pingback from webagentur-meerbusch.de Die Z-Index-CSS-Eigenschaften: Eine umfassende Übersicht, With the eye for Design den wir bringen Klein und Mittelstand mit Erfolg ins Internet
  29. Pingback from blog.barkerdesign.com Barker Design | Graphic & Web Development » Blog Archive » The Z-Index CSS Property: A Comprehensive Look
  30. Pingback from osmn00.com 堂主,WEB前端开发技术 » 博客文章 » 全透视:CSS Z-index 属性
  31. Pingback from variedades.salteadoresdaarca.com The Z-Index CSS Property: A Comprehensive Look | Variedades e Bookmarks
  32. Pingback from unicac.cn » 全透视:CSS Z-index 属性-Unicac’s Blog
  33. Pingback from grumpygit.org The Z-Index CSS Property: A Comprehensive Look | Grumpy Git . org | Photoshop
  34. Pingback from feelcreative.com.au JQuery Share Box - Good use of z-index | Feel Creative
  35. Trackback from PimpThisBlog.com Create fancy share box with CSS and jQuery
  36. Pingback from oorch.com 25 New jQuery Plugins and Tutorials | oOrch Blog
  37. Pingback from osmn00.com 全透视:CSS Z-index 属性 « 堂主,WEB前端开发技术
  38. Pingback from designshack.co.uk 20 Simple jQuery Tricks | Design Shack
  39. Pingback from ajaxdump.com Create fancy share box with CSS and jQuery | ajaxdump
  40. Pingback from techtunez.com 20 jQuery Plugins/Techniques For Web Designers And Developers | TechTuneZ
  41. Pingback from speckyboy.com 25 Fresh and New jQuery Plugins and Tutorials : Speckyboy Design Magazine
  42. Pingback from designersandbox.com 22 CSS/jQuery Solutions Showcase Tutorials, Beautify Your WebSite Now | Designer Sandbox
  43. Pingback from zhongkun.72pines.com » The Z-Index CSS Property zhongkun
  44. Pingback from myowndeveloper.com 25 Fresh and New jQuery Plugins and Tutorials | Filipino Freelance Web Masters : Filipino Web Designers and Web Application Developers, SEO, Researchers, Marketers and CMS Specialists

Add comment

   
        Country flag
biuquote
Loading