Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PHP Ajax Cookbook

You're reading from   PHP Ajax Cookbook Over 60 simple but incredibly effective recipes to Ajaxify PHP websites with this book and ebook

Arrow left icon
Product type Paperback
Published in Dec 2011
Publisher
ISBN-13 9781849513081
Length 340 pages
Edition Edition
Languages
Arrow right icon
Toc

Table of Contents (16) Chapters Close

PHP Ajax Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
1. AJAX Libraries FREE CHAPTER 2. Basic Utilities 3. Useful Tools Using jQuery 4. Advanced Utilities 5. Debugging and Troubleshooting 6. Optimization 7. Implementing Best Practices to Build Ajax Websites 8. Ajax Mashups 9. iPhone and Ajax Index

Creating tab navigation


jQuery UI is built from the core interaction plugins of jQuery. As a high-level framework, it makes creating effects and animation easy for every developer. Now we will build a tab navigation using jQuery UI.

Getting ready

First of all, we need to include the jQuery library from www.jquery.com, if we haven't done it in the preceding recipe. Then, we can download jQuery UI library from www.jqueryui.com/download. On this page, we can download specific modules or the whole library. We can select the theme we like or create our own one with advanced theme settings. For now, we will select the whole library with the ui-lightness theme.

How to do it...

  1. Now we are ready for coding. Let's start with the HTML part. This part will define a navigation element with three tabs and one accordion.

    <body> 
      <div id="navigation"> 
        <ul> 
          <li><a href="#tabs-1">Home</a></li> 
          <li><a href="#tabs-2">Our Books</a></li> 
           <li><a href="ajax/shop.html">Shop</a></li> 
        </ul> 
        <div id="tabs-1"> 
          <p>Lorem ipsum dolor 1</p>
        </div>
        <div id="tabs-2">
    
          <p>Lorem ipsum dolor 2</p> 
        </div>
      </div> 
    </body> 
  2. When the HTML is ready, we can continue with CSS and JavaScript CSS styles in the <head> tag, as shown in the following code:

    <head>
      <link href="css/ui-lightness/jquery-ui.custom.css" 
      rel="stylesheet" />
    </head>
  3. We will add JavaScript before closing the <body> tag:

      <script src="js/jquery.min.js"></script> 
      <script src="js/jquery-ui.custom.min.js"></script> 
      
      <script> 
        $(document).ready(function(){
          $('#navigation').tabs();
        });
      </script> 
    </body>
  4. Our result looks like the following:

How it works...

The downloaded jQuery UI contains the whole CSS content of the selected theme (jquery-ui.custom.css). All we need to do is to include it in the <head> tag:

...
  <link href="css/ui-lightness/jquery-ui.custom.css" 
  rel="stylesheet" />

After CSS, we include jQuery and the jQuery UI library:

  <script src="js/jquery.min.js"></script> 
  <script src="js/jquery-ui.custom.min.js"></script> 

The JavaScript part is really simple:

$('#navigation').tabs();

It is important to fit the required HTML structure. Each hyperlink is targeting the HTML content in selected <div> tags. To create a relation between them we will use #id in each hyperlink and the ID of the selected <div> tag (for example, tabs-1).

There is an exception in the third tab, which loads the requested data via Ajax. In this case, we do not define any target area, as it will be created automatically. As you can see, using the Ajax in jQuery UI is really easy and comfortable.

There's more...

jQuery UI offers us a lot of options. We can use just a default functionality as was presented in the preceding code snippet or some additional functionality:

Content via Ajax:

$( "#navigation" ).tabs({ajaxOptions: {} });

Open on mouseover:

$( "#navigation" ).tabs({event: "mouseover"});

Collapse content:

$( "#navigation" ).tabs({collapsible: true});

Sortable:

$( "navigation" ).tabs().find( ".ui-tabs-nav" ).sortable({ axis: "x" });

Cookie persistence:

$( "#navigation" ).tabs({cookie: { expires: 1 }});

See also

Chapter 3, Useful tools using jQuery

You have been reading a chapter from
PHP Ajax Cookbook
Published in: Dec 2011
Publisher:
ISBN-13: 9781849513081
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image