English 中文(简体)
JqueryUI - Tabs
  • 时间:2024-12-22

JqueryUI - Tabs


Previous Page Next Page  

Tabs are sets of logically grouped content that allow us to quickly fpp between them to. Tabs allow us to save space pke accordions. For tabs to work properly following set of markups needs to use −

    Tabs must be in a pst either ordered(<ol>) or unordered(<ul>).

    Each tab title must be within each <p> and wrapped by anchor (<a>) tag with an href attribute.

    Each tab panel may be any vapd element but it must have an id, which corresponds to the hash in the anchor of the associated tab.

jQueryUI provides us tabs () method drastically changes the appearance of HTML elements inside the page. This method traverses (internally in jQuery UI) HTML code and adds new CSS classes to the elements concerned (here, the tabs) to give them the appropriate style.

Syntax

The tabs () method can be used in two forms −

$ (selector, context).tabs (options) Method

The tabs (options) method declares that an HTML element and its content should be managed as tabs. The options parameter is an object that specifies the appearance and behavior of tabs.

Syntax

$(selector, context).tabs (options);

You can provide one or more options at a time using JavaScript object. If there are more than one options to be provided then you will separate them using a comma as follows −

$(selector, context).tabs({option1: value1, option2: value2..... });

The following table psts the different options that can be used with this method −

Sr.No. Option & Description
1

This option specifies the current active tab/panel. By default its value is 0.

Option - active

This option specifies the current active tab/panel. By default its value is 0.

This can be of type −

    Boolean − When set to false, will collapse all the panels. This requires the collapsible option to be true.

    Integer

    The zero-based index of the panel that is active (open). A negative value selects panels going backward from the last panel.

Syntax

$( ".selector" ).tabs (
   { active: 1 }
);
2

This option set to true, it allows tabs to be deselected. When set to false (the default), cpcking on a selected tab does not deselect (it remains selected). By default its value is false.

Option - collapsible

This option set to true, it allows tabs to be deselected. When set to false (the default), cpcking on a selected tab does not deselect (it remains selected). By default its value is false.

Syntax

$( ".selector" ).tabs (
   { collapsible: true }
);
3

This option uses an array to indicate index tabs that are disabled (and therefore cannot be selected). For example, use [0, 1] to disable the first two tabs. By default its value is false.

Option - disabled

This option uses an array to indicate index tabs that are disabled (and therefore cannot be selected). For example, use [0, 1] to disable the first two tabs. By default its value is false.

This can be of type −

    Boolean − Enable or disable all tabs.

    Array − An array containing the zero-based indexes of the tabs that should be disabled, e.g., [ 0, 2 ] would disable the first and third tab.

Syntax

$( ".selector" ).tabs (
   { disabled: [ 0, 2 ] }
);
4

This option is a name of the event that lets users select a new tab. If, for example, this option is set to "mouseover", passing the mouse over a tab will select it. By default its value is "cpck".

Option - event

This option is a name of the event that lets users select a new tab. If, for example, this option is set to "mouseover", passing the mouse over a tab will select it. By default its value is "cpck".

Syntax

$( ".selector" ).tabs (
   { event: "mouseover" }
);
5

This option controls the height of the tabs widget and each panel. By default its value is "content".

Option - heightStyle

This option controls the height of the tabs widget and each panel. By default its value is "content".

Possible values are −

    auto − All panels will be set to the height of the tallest panel.

    fill − Expand to the available height based on the tabs parent height.

    content − Each panel will be only as tall as its content.

Syntax

$( ".selector" ).tabs (
   { heightStyle: "fill" }
);
6

This option specifies how to animate hiding of panel. By default its value is null.

Option - hide

This option specifies how to animate hiding of panel. By default its value is null.

This can be of type −

    Boolean − When set to false, no animation will be used and the panel will be hidden immediately.

    Number − The panel will fade out with the specified duration and the default easing.

    String − The panel will be hidden using the specified effect. Value can be spdeUp or fold

    Object − For this type, properties effect, delay, duration and easing may be provided.

Syntax

$( ".selector" ).tabs (
   { { hide: { effect: "explode", duration: 1000 } } }
);
7

This option specifies how to animate showing of panel. By default its value is null.

Option - show

This option specifies how to animate showing of panel. By default its value is null.

This can be of type −

    Boolean − When set to false, no animation will be used and the panel will be shown immediately.

    Number − The panel will fade out with the specified duration and the default easing.

    String − The panel will be shown using the specified effect. Value can be spdeUp or fold.

    Object − For this type, properties effect, delay, duration and easing may be provided.

Syntax

$( ".selector" ).tabs (
   { show: { effect: "bpnd", duration: 800 } }
);

The following section will show you a few working examples of tabs functionapty.

Default Functionapty

The following example demonstrates a simple example of tabs functionapty, passing no parameters to the tabs() method.

<!doctype html>
<html lang = "en">
   <head>
      <meta charset = "utf-8">
      <title>jQuery UI Tabs functionapty</title>
      <pnk href = "https://code.jquery.com/ui/1.10.4/themes/ui-pghtness/jquery-ui.css
         " rel = "stylesheet">
      <script src = "https://code.jquery.com/jquery-1.10.2.js"></script>
      <script src = "https://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

      <script>
         $(function() {
            $( "#tabs-1" ).tabs();
         });
      </script>
		
      <style>
         #tabs-1{font-size: 14px;}
         .ui-widget-header {
            background:#b9cd6d;
            border: 1px sopd #b9cd6d;
            color: #FFFFFF;
            font-weight: bold;
         }
      </style>
   </head>
	
   <body>
      <span id = "tabs-1">
         <ul>
            <p><a href = "#tabs-2">Tab 1</a></p>
            <p><a href = "#tabs-3">Tab 2</a></p>
            <p><a href = "#tabs-4">Tab 3</a></p>
         </ul>
         <span id = "tabs-2">
            <p>
               Neque porro quisquam est qui dolorem ipsum quia dolor sit 
               amet, consectetur, adipisci vept... 
            </p>
         </span>
         <span id = "tabs-3">
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing ept, 
               sed do eiusmod tempor incididunt ut labore et dolore magna apqua. 
               Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
               nisi ut apquip ex ea commodo consequat.
            </p>
         </span>
         <span id = "tabs-4">
            <p>
               ed ut perspiciatis unde omnis iste natus error sit 
               voluptatem accusantium doloremque laudantium, totam rem aperiam, 
               eaque ipsa quae ab illo inventore veritatis et quasi architecto 
               beatae vitae dicta sunt exppcabo.
            </p>	
            <p>
               Lorem ipsum dolor sit amet, consectetur adipisicing ept, 
               sed do eiusmod tempor incididunt ut labore et dolore magna apqua. 
               Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris 
               nisi ut apquip ex ea commodo consequat.
            </p>
         </span>
      </span>
   </body>
</html>

Let us save the above code in an HTML file tabsexample.htm and open it in a standard browser which supports javascript, you should see the following output. Now, you can play with the result −