jQuery Tabs Plugin with Images

Technorati Tags:

The jQuery tabs is a nice plugin.  But depending on how creative your site is and how aesthetic it’s function needs to be, a lot of times you’re not going to use it as is because it simply looks like a “developer designed the tabs”.  So, to pretty up the tabs, you can simply set the images and then when you click just change out the image to show an active image using jQuery selectors.

Here’s how I did it:

<script type="text/javascript">
     $(function() {
 
         function handleTabSelect(event, tab) {
             if (tab.index == 0) {
                 // tab 1 selected
                 $('img#imgTab1').attr({ src: "/Content/Images/Product/tab1Selected.png" });
                 $('img#imgTab2').attr({ src: "/Content/Images/Product/tab2Unselected.png" });
             }
             else if (tab.index == 1) {
                 // tab 2 selected
                 $('img#imgTab2').attr({ src: "/Content/Images/Product/tab2Selected.png" });
                 $('img#imgTab1').attr({ src: "/Content/Images/Product/tab1Unselected.png" });
             }
         }
 
         // Set Tab Constructor values & initialize tabs
         $('#tabs').tabs({ selected: '0', 
                         fx: { opacity: 'toggle', duration: 'slow' }, select: handleTabSelect });
 
     });
 </script>

The html:

<div id="tabs">
    <ul class="tabImages">
        <li runat="server"><a href="#Tab1Content"><span><img id="imgTab1" width="262" height="67" 
        src="/Content/Images/product/tab1_Selected.png" /></span></a></li>
        <li runat="server"><a href="#Tab2Content"><span><img id="imgTab2" width="262" height="67" 
        src="/Content/Images/Product/tab2_Unselected.png" /></span></a></li>
    </ul>
   <div id="TabsMain">
        <div id="Tab1Content">
            <asp:ContentPlaceHolder ID="cphTab1Content" runat="server" />
        </div>
        <div id="Tab2Content">
            <asp:ContentPlaceHolder ID="cphTab2Content" runat="server" />
        </div>
    </div>
</div>
 
So the handleTabSelect event handler is fired whenever you click on a tab.  Based on what tab was clicked (index), it will switch out the images.  Pretty simple but the tricky part is figuring out how to do that in jQuery if you’re not as familiar with the syntax and are new to jQuery.  In jQuery as everyone knows, you can manipulate (do something with…add, changes, remove) any part of the DOM in your page (html elements, css elements, and more) but again, it’s knowing the jQuery syntax first before knowing how to manipulate the DOM with jQuery so study up!.  And this is how you would manipulate the images here in this context with jQuery tabs.
 
And obviously the tab1_Selected.png and tab2_Unselected.png speak for themselves..you’re just going to change the image to show a selected vs. not in jQuery once a certain tab has focus.
 
I highly recommend (I have these) these 3 books if you’re going to start jQuery:

Learning jQuery 1.3

ISBN: 1847196705
ISBN-13: 9781847196705

jQuery UI 1.6: The User Interface Library for jQuery

ISBN: 1847195121
ISBN-13: 9781847195128

 
The Learning jQuery book (3rd one above) is a tad bit outdated but not by much.  jQuery is in v1.3 but the other is still overall a good book and well worth it to learn jQuery no matter what version.  I recommend having all 3 of these books.  The internet is nice but you will get up to speed at least with jQuery in my opinion with these books first and internet as a supplement.
 
And as you can see, there’s no excuse not to use tableless design in today’s presentation layer.  There’s no point It’s so much cleaner and easier to maintain.  Tables add so much damn clutter to the page.  Learn CSS tableless design.  Not easy at first but learn it.  It shows you care about not only standards but clean maintainable code in the presentation layer.  And ASP.NET MVC also works well when you have a nice clean page using tableless design in your views.  I cannot imagine positioning all this with tables anymore in my career and I will not if I can’t help it.
 
I’m not going to post the CSS, because all boils down to is creating the class ids for tabs (required by the jQuery tabs plug-in to work), tabImages, TabsMain, Tab1Content, and Tab2Content.  It’s pretty self explanatory and if not just comment and I’ll explain in this post.
 


Print | posted on Saturday, May 02, 2009 8:25 PM

Comments on this post

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Do you have a demo or should we guess what the output looks like based on the code? Demo would be nice!
Left by Trevor on May 05, 2009 10:19 AM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
I've managed to get the images to work. However I have a drop down nav link right above this div & it's currently showing up behind the two images. How can I correct this?
Left by JA3 on May 29, 2009 5:40 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
@JA3

what do you mean you have a dropdown nav link. I do not understand what you mean. Post some code.
Left by Dave Schinkel on May 30, 2009 9:34 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Thanks for this.

So far it works a treat.

Appreciate your work and the fact you share.

Dave
Left by DJ on Jun 10, 2009 6:50 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Nevermind, I ended up going with just text.
Left by JA.3 on Jun 12, 2009 4:56 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
thanks for nice information ihave research this for long time and found the answer here
Left by asher on Jun 30, 2009 11:59 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Is there a demo???
Left by cancel bubble on Aug 07, 2009 1:39 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
I have 9 different tabs on about 20 different pages. Add that up and it's a lot of lines of code based on this example. Would there be an easier way to reset the off-state tab when another is selected? This is not tested, but looking for something simpler...

function handleTabSelect(event, tab) {

if (tab.index !== tab.index) {
// tab 1 selected
$("img#imgTab"+tab.index).attr({ src: "/images/tabs/triggers/tab-"+tab.index+".gif" });
}
else if (tab.index == tab.index) {
// tab 2 selected
$("img#imgTab"+tab.index).attr({ src: "/images/tabs/triggers/tab-"+tab.index+"-on.gif" });
}
}
Left by Merle on Aug 12, 2009 10:37 AM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
This is what I came up with. If anyone has a better way to do this, I'm open to hear about it. It's not that much JavaScript in all. You jsut have a LOT of pages with tabs.
Left by Dave Schinkel on Aug 12, 2009 9:15 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
I just wanted to reduce the number of images, lines of code and styles. So what I ended up doing was using a transparent gif for each tab. Then each tab is assigned a background image. When in the active/selected state I move the image up on a y-axis. The background assignment for each tab is done in a loop.

Here is my JS & CSS:



CSS:

li.ui-state-active {background-position: 0px 41px;}
Left by Merle on Aug 13, 2009 6:46 AM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Here is my JS


$(document).ready(function(){
// Set Tab Constructor values & initialize tabs
$('#tabs').tabs({ selected: '0',
fx: { opacity: 'toggle', duration: 'fast' }}
);

var i=0;
for (i=0;i<=10;i++)
{
$("#tabs li#tab"+i).attr({ style: "background-image:url(/images/tabs/tab-"+i+".gif)" });
}
});

Left by Merle on Aug 13, 2009 6:48 AM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Tabs can be added dynamically, the "selected" tab can be acquired, idTabs can be binded to different events like mouseover.

--------------------------------------
70-236 questions | 650-393 questions | 650-251 questions
Left by tina90 on Sep 10, 2009 2:40 AM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Sorry, I have not had time to put up a demo. I will eventually. I'm just very swamped at the moment. I suppose I could camtasia it. Maybe I'll do that. I've got this working in our redesign project at work.
Left by Dave Schinkel on Sep 30, 2009 7:20 PM

# re: jQuery Tabs Plugin with Images

Requesting Gravatar...
Thanks for the useful information. I've come across that issue quite a few times and was wondering how that could be implemented in many of our clients sites.
Left by Modi on Oct 07, 2009 8:14 AM

Your comment:

 (will show your gravatar)
 
Please add 8 and 3 and type the answer here: