Create fancy share box with CSS and jQuery

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!

More articles in Blog archive or elsewhere
Advertisement

35 Comment(s)

Davide Espertini

Davide Espertini 13 Jul 2009 #

Great plugin Janko! Well done! Laughing

Wallace

Wallace 13 Jul 2009 #

Nice touch, lovin it.

webmasterdubai

webmasterdubai 13 Jul 2009 #

really nice work and good jquery plugin.

Geld Lenen

Geld Lenen 13 Jul 2009 #

Great tutorial! Nice and efficient way to display your social bookmarks.

Andreas

Andreas 13 Jul 2009 #

Note that there is still a counter jquerys each function, so you can write it like this:

$(element).find("li").each(function(i){
            $(this).css("z-index", 10- i);
            if (i>0)
                $(this).css("left", i * 24 + 100);
});

Also px isn't needed when setting css value

Janko

Janko 13 Jul 2009 #

Thanks everyone!

Andreas, thanks for nice optimization tip!

Kawsar Ali

Kawsar Ali 13 Jul 2009 #

O really neat. Thanks for showing how to turn it into plugin .

Marko

Marko 13 Jul 2009 #

Good work again. Might be using it somewhere.

John Campbell

John Campbell 14 Jul 2009 #

Awesome post! I have to find a way to use this in one of my upcoming projects. Great work!

Knoxville Website Design

Knoxville Website Design 14 Jul 2009 #

Nice little effect you have there, thanks for sharing the code.

TutorialFeed

TutorialFeed 14 Jul 2009 #

Nice Tut Buddy. Keep it up. Thanks a lot. I'll try to use in my pages.

Thanks
Tutorialfeed

Mario

Mario 14 Jul 2009 #

Why use jquery when we can use the css2?? we can make a great slim code? if you wolud known the procedure you can leave a comment in my blog in the first post(welcome) (link in nickname) or here..

enjoy it!!

by

Janko

Janko 14 Jul 2009 #

Thanks guys!!

Mario: You won't be able to place buttons in such order and positions only with css (or at least I don't know it's possible). For instance, you need a reverse z-index on buttons (the last button should have the smallest z-index and vice versa).

g17

g17 14 Jul 2009 #

You are just great. I am using your icons even on political website Smile)

Janko

Janko 15 Jul 2009 #

g17: Nice to see Handycons there Smile

Mario

Mario 15 Jul 2009 #

exactly Janko whit z-index i'm makeing a version in my blog...stay tuned

Ali Hussain

Ali Hussain 15 Jul 2009 #

Wow. Nice share. Downloading and installing it on my blog now

Webdesign Santhos

Webdesign Santhos 19 Jul 2009 #

Great tutorial! I can imagine using this technique in many different cases also! Great work!

Dzinepress

Dzinepress 19 Jul 2009 #

that's really cool, i must experiment in my coming social bookmarking website, is this available in any wordpress blog?

Web Solutions

Web Solutions 20 Jul 2009 #

Well i like your overlaping concept.
i will get the help from your code in future projects
thanks for sharing this .

Stefan

Stefan 24 Jul 2009 #

I will use your code on one future project of mine... I think it's a unusual share box and I will focus some users of my site with your sharebox

Graficando

Graficando 25 Jul 2009 #

Oh yes may job is now online

thank you

Janko

Janko 25 Jul 2009 #

Thanks everyone for the comments Smile

Mario (Graficando): Good work, but example isn't quite the same as this one. The point of using jQuery is to be able to add items dynamically. By hardcoding z-index in your CSS you won't be able to do that. If I want to add more buttons, I can just add more LI elements. The other thing is that buttons don't slide but are rather being "send to front" on hover.

@w

@w 26 Jul 2009 #

thanks

stej

stej 29 Jul 2009 #

Hm, I wonder if guidesigner.net/.../ is your article or they simply stole it Foot

Janko

Janko 29 Jul 2009 #

stej: it's stolen, thanks for letting me know.

Oliveoil

Oliveoil 29 Jul 2009 #

you are genius !! lov it ^^

thank you for sharing !

cheer from TOKYO !

SJL Web Design

SJL Web Design 31 Jul 2009 #

Very nice, I'm bookmarking this for my new site. Thanks for sharing it with us!

Dusan

Dusan 04 Aug 2009 #

Svaka cast na text-u! Dobro objasnjeno a jos bolje radi Smile

Usput jedno pitanje vezano za ovo, pocetnik sam pa ce mozda i glupo izgledati, ako imas vremena bilo bi lepo da vidim odgovor, ako ne nista Smile kada ovo kombinujem na istoj stranici sa jcarousel, prestaje da radi, kao da nema sharebox.js ... ponasa se kao i pre dodavanja iste ...

Poz i sve najbolje! Iznenadjen sam kvalitetom blog-a, drago mi je da vidim sve vise srba sa ovakvim znanjem u ovoj oblasti, a i nadam se da cu jednog dana bar upola znati! Smile

Ivan Minic

Ivan Minic 05 Aug 2009 #

Cute Smile Very effective and space preserving.

MJ Stapleford Corporate Identity

MJ Stapleford Corporate Identity 21 Aug 2009 #

This is really nice. I agree with Ivan the way it preserves space is really cool. Just adds that little bit of extra style that helps to impress website visitors. Thank you for sharing this.

knoxville web design

knoxville web design 05 Sep 2009 #

Really like the layout. Good structure. It may just be my monitor but some of the colors around the images aren’t the same as the background colors. Other than that I really think its a great site.

Juergen

Juergen 10 Nov 2009 #

Hi,
the demo looks very nice. I try to follow your instructions to implement it. The only problem I see is that I'm not familar with jQuery.

FlashDo

FlashDo 07 Jan 2010 #

Very nice & useful product. I used it in my website. Look at this page:

www.flashdo.com/.../97

John P.

John P. 25 Jan 2010 #

Nice share box! My current icons look kinda dull! Yours are nice and colorful. Thanks for the contribution.

Comments are closed
via Ad Packs
9292