Split.js is a lightweight, unopinionated utility for creating adjustable split views or panes. Demo.
No dependencies or markup required, just two or more elements with a common parent. Views can be split horizontally or vertically, with draggable gutters inserted between every two elements.
Install with Bower:
bower install split.js
Or clone from Github:
git clone https://github.com/nathancahill/Split.js.git
Split(<selector[]> selectors, <options> options?)
Options | Type | Default | Description |
---|---|---|---|
sizes | Array | Initial sizes of each element in percents or CSS values. | |
minSize | Number or Array | 100 | Minimum size of each element. |
gutterSize | Number | 10 | Gutter size in pixels. |
snapOffset | Number | 30 | Snap to minimum width offset in pixels. |
direction | String | 'horizontal' | Direction to split: horizontal or vertical. |
cursor | String | 'grabbing' | Cursor to display while dragging. |
onDrag | Function | Callback on drag. | |
onDragStart | Function | Callback on drag start. | |
onDragEnd | Function | Callback on drag end. |
A split with two elements, starting at 25% and 75% wide with 200px minimum width.
Split(['#one', '#two'], {
sizes: [25, 75],
minSize: 200
});
A split with three elements, starting with even widths with 100px, 100px and 300px minimum widths, respectively.
Split(['#one', '#two', '#three'], {
minSize: [100, 100, 300]
});
A vertical split with two elements.
Split(['#one', '#two'], {
direction: 'vertical'
});
Specifying the initial widths with CSS values. Not recommended, the size/gutter calculations would have to be done before hand and won't scale on viewport resize.
Split(['#one', '#two'], {
sizes: ['200px', '500px']
});
In being non-opionionated, the only CSS Split.js sets is the widths or heights of the elements. Everything else is left up to you. However, here's some basic CSS to style the gutters with:
.gutter {
background-color: #eee;
background-repeat: no-repeat;
background-position: 50%;
cursor: move;
cursor: grab;
cursor: -moz-grab;
cursor: -webkit-grab;
}
.gutter.gutter-horizontal {
background-image: url('grips/vertical.png');
}
.gutter.gutter-vertical {
background-image: url('grips/horizontal.png');
}
Split.js also works best when the elements are sized using border-box
. The split
class would have to be added manually to apply these styles:
.split {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
And for horizontal splits, floating the elements with 100% height is useful:
.split, .gutter.gutter-horizontal {
height: 100%;
float: left;
}
Overflow can be handled as well:
.split {
overflow-y: auto;
overflow-x: hidden;
}
This library uses calc(), box-sizing and getBoundingClientRect(). This features are supported in the following browsers:
19+ ✔ | 4+ ✔ | 9+ ✔ | 32+ ✔ | 7+ ✔ |
Gracefully falls back in IE 8 and below to only setting the initial widths/heights and not allowing dragging.
Copyright (c) 2015 Nathan Cahill
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.