jQuery Splitter Plugin

Demos

Known Bugs

Getting Started

Take the demo closest to what you need, read the instructions in the CSS and code, add content, and pretty up the styles. Go ahead, I'll wait. What, you're not done yet and want some more information? Here's a quick tour. Markup for a vertical splitter looks like this:

 <div id="MySplitter">
   <div id="LeftPane"> Left content goes here </div>
   <div id="RightPane"> Right content goes here </div>
 </div> 

The top-level div encloses two child divs that serve as the left and right panes. The plugin doesn't care about the id names, but they simplify applying styles in a stylesheet. The splitter documentation below refers to the first pane as pane A, since it is either the left pane of a vertical splitter or the top pane of a horizontal splitter. Similarly, the second pane is pane B, the right or bottom pane. The plugin dynamically adds the splitbar that goes between the panes. When the documentation refers to the size of a pane, it means the variable dimension--width for a vertical splitter and height for a horizontal one.

Any splitter will also have stylesheet rules to provide visual styles and dimensions. It should have distinctive styling for the splitbar with visual clues that the bar can be moved by the user. The demo pages use color changes and several small bitmaps for the splitbars to make them stand out.

To create the splitter, put a line of code in a .ready() handler to select the MySplitter div in a jQuery object and pass it to the splitter plugin:

 $().ready(function(){
   $("#MySplitter").splitter({type: 'v'});
 });

Congratulations, there's a vertical splitter in your document!

Moving the splitbar

Users can move the splitbar in three ways:

  1. Move the mouse over the splitbar; the cursor will change and the splitbar will indicate it is selected. Click and drag the splitbar to move it.
  2. Specify an HTML accessKey when you call the function; e.g., $().splitter({accessKey: "I"}). Press that key (e.g, Alt-Shift-I on a PC, Cmd-Shift-I on a Mac) to select the splitbar, then use the arrow keys to move it.
  3. Press tab to cycle through the tab order until the splitbar is selected, then use the arrow keys to move it. Press tab again to deselect the splitbar.

Splitter options described below can modify most aspects of this behavior, such as the type of cursor used on the splitter, the tab order of the splitbar and whether the splitbar is in the page's tab order at all. By setting size limits on the two panes, you can prevent the splitter from making one of the panes too big or small.

It is also possible to move the splitbar position programmatically. Trigger the splitter's resize event, passing it an array with a single number that indicates the new splitbar position, in pixels from the top/left edge:

 $("#MySplitter").trigger("resize", [ 200 ]);

Movement of the splitbar through this type of resize is still subject to any size and position constraints that were set by options or stylesheet rules.

Setting the initial splitbar position

By default, the splitbar is set to the center of the splitter area at initialization, giving equal area to both panes. To set a different starting splitbar position, use the initA (left/top pane) or initB (bottom/right pane) options to the plugin. Only one of the values should be set. If you specify the value true, the plugin will obtain the size from the width/height property set though the stylesheet. Alternatively, you can use initA or initB to specify the size of the pane (in pixels).

Setting initA or initB when calling the plugin also tells it which pane should be resized when the splitter container is resized. By default, pane A maintains its size and B will grow or shrink unless the minB or maxB constraints force A to change. By specifying initB in the plugin options, pane B maintains its size and pane A is resized.

Resizing the splitter container

The splitter plugin supports resizing the splitter container that holds both panes, but it must be notified of a change via a resize event. Internet Explorer does this automatically, but other browsers do not. In the example above, #MySplitter is defined to be a 400x300 pixel fixed width. To resize it to 400x400 pixels, you could use this code:

 $("#MySplitter").css("height", "400px").trigger("resize");

If the splitter container has a fluid width, it extends the full width of its containing element. That means it will automatically resize whenever the parent element's size changes. However, the splitter needs to be notified when this occurs so that it can adjust the panes as well. The most common need for a resize is when the user changes the size of the browser's window; it just takes a few extra lines in the .ready() handler to pass along the window's resize event to the splitter whenever the browser window is resized:

 $(document).ready(function(){
   $("#MySplitter").splitter({direction: 'v'});
   $(window).bind("resize", function(){ 
	  $("#MySplitter").trigger("resize");
   });
 });

You can create a semi-fluid splitter container by setting a minimum and maximum size for the container. This change to the style for #MySplitter would allow its width to vary between 300 and 600 pixels, based on the size of the parent container:

 #MySplitter { min-width: 300px; max-width: 600px; height: 300px; }

The 3-pane splitter demo demonstrates resizing the splitter container to fit the height and width of the browser window. The splitter plugin automatically sends a resize event to each pane whenever the splitbar is moved or the outer container is resized, so no extra work is needed to support nested splitter containers.

For performance reasons, the splitter plugin caches the size of the padding and borders for the splitter container and both panes when the splitter is created. Do not change these values after creating the splitter.

Setting minimum and maximum pane sizes

When no size constraint options are specified for either pane, the plugin allows the splitbar to be moved so it totally obscures the content of one of the panes. A scroll bar or padding may still be visible however. It is not possible to move the splitbar to make one of the panes disappear from the face of the screen.

The size of the two panes in the splitter (and thus the splitbar's range of motion) can be constrained using either CSS min-width and max-width properties on the element panes or by the options object passed to the plugin when the splitter is created. For CSS properties, use pixel (px) units so the plugin can parse them. The options object values are also numbers assumed to be in pixels.

Styling the splitbar

When the plugin initializes the splitter, it creates the splitbar element that goes between the two panes. It automatically assigns the class vsplitbar to vertical splitters and hsplitbar to horizontal ones. (The splitbar class names need to be unique for vertical vs. horizontal splitbars so that nested splitters can be accomodated.) This allows you to create stylesheet rules for the splitbar. You can change the class name by passing the splitbarClass string to the plugin.

By default, when the mouse is over the splitbar it changes to a two-headed arrow cursor in the direction of motion (e-resize or n-resize). To override the default, add a cursor property to the splitbar's stylesheet entry. A custom cursor such as an .ani or .cur file from a URL can be used for special effects. See the Skinny Splitbar demo for an example of custom cursors.

Options reference

Most of the options that control the behavior of the splitter plugin can be located in either of two places. You can specify them in the object passed into the plugin when the splitter is created, or in stylesheet properties for the elements used by the splitter. The descriptions below are for the names used by the options object, but if that option can also be controlled by styles it is described in that section as well.

type
Determines whether the splitter is split in the horizontal or vertical direction. The input value should be a single-character string, either 'h' or 'v'. If none is specified, a vertical splitter is created.
initA, initB
Define one (but not both!) of these numbers to set the size of one pane and thus the splitbar's initial position. If the value is set to true, the plugin will retrieve the size of that pane from the width/height stylesheet rules. Otherwise, pass in a number representing the initial size (pixels) of pane A or pane B. When the splitter container is resized, pane A is usually held at its current size and pane B grows or shrinks as a result. When initB is specified, however, the plugin attempts to keep pane B constant and pane A changes size. However, pane B may still be resized if pane A reaches a min/max size limit and pane B is not at its minimum size. If neither initA nor initB are specified, the splitbar is placed in the center of the splitter area.
minA, maxA, minB, maxB
Set these numbers to constrain the splitbar's travel and keep a pane from getting too large or small. The values are in pixels, and should include the size of any padding placed on the pane. The minimum and maximum values can also be set using the min-height and min-width stylesheet properties (in pixels) on the two panes. If you provide values for both panes, make sure that they do not conflict. For example, a vertical splitter that is 200 pixels wide cannot have both minA:120 and minB:120 or maxA:50 and maxB:50.
tabIndex
The splitbar can be moved by pressing tab until the splitbar is selected, then pressing arrow keys to move the bar. To change the tab order, provide a number for tabIndex (0 to 32000); non-unique tabIndex values are tabbed in HTML source order. The default tabIndex is 0; since focusable elements have tabIndex=0 by default, most will appear in HTML source order as well. To take the splitbar out of the tab order completely, use -1 for tabIndex. (The splitbar can still be selected by keyboard if an accessKey is specified.)
accessKey
This specifies a key that can be pressed in combination with Alt-Shift (Cmd-Shift on a Mac) to select the splitbar. An accessKey of '|' makes Alt-Shift-| the selector and 'I' makes Alt-Shift-I the selector. Note that many letters are already used for the browser's own menu accelerators; menu letters vary by browser and sometimes the accessKey does not have priority over the menu. If no accessKey is provided, the splitbar can still be selected by tabbing. To disable keyboard splitter operation, set tabIndex to -1 and do not define an accessKey.
activeClass
When a splitbar move is in progress, the plugin adds the activeClass class to the splitbar element. By default it is "active". This class name is used in a stylesheet to provide special splitbar effects such as a background color change.
splitbarClass
The plugin adds the splitbar element to the splitter container and sets a class name on the element so that it can be referenced in stylesheet rules. By default the class name is "vsplitbar" for vertical splitters and "hsplitbar" for horizontal ones. Pass in a string for splitbarClass to override this name.
pxPerKey
When the keyboard is used to move the splitbar, this number specifies how many pixels the splitbar should move on each keypress. The default is 5 pixels. If the splitbar reaches one of the limits specified for the panes being split, it will move as far it can (e.g, it may move 3 out of the 5 pixels before being stopped).
keyGrowA, keyShrinkA
These values represent numeric key codes recognized when the keyboard is used to move the splitbar. keyGrowA makes pane A (the left or top pane) larger; by default it is the right-arrow key (vertical splitter) or down-arrow key (horizontal splitter). keyShrinkA defines the key codes for the opposite direction.