Original JS partition effect, object-oriented, easy to use. I have also seen some column splitting effects on the Internet, and there is also a jquery plug-in jquery. splitter. js, But they basically did not solve a problem: if there is an iframe on the page, when the split line is dragged through iframe, the mouse does not listen, I once posted a post to discuss this problem. This example uses a small trick to solve this problem and make the drag smooth.
The Code is as follows:
Splitter demo
Script
/*
* Splitter. js
* Author: sunxing007
* Http://blog.csdn.net/sunxing007
* Date: 08/26/2009
**************************************** **************************************** ******
The css script below is needed for the html page when using splitter. js, please save
It as splitter.css, and modify it carefully.
**************************************** **************************************** ******
# Splitter_container {
Width: 100%;
Height: 100%;
Border: solid # eee 1px;
Margin: 0px;
Padding: 0px;
Overflow: hidden;
}
# Splitter_left_panel {
Width: 300px;
Height: 100%;
Float: left;
Border: solid blue 0px;
}
# Splitter_bar {
Width: 8px;
Height: 100%;
Float: left;
Background-color: # ccc;
Cursor: col-resize;
}
# Splitter_right_panel {
Height: 100%;
Padding-top: 10px;
}
**************************************** **************************************** ******
How to use this splitter?
**************************************** **************************************** ******
Right panel
-->
**************************************** **************************************** ******
*/
/** This is a helper function used to get the dom element specified by id **/
Function $ (id) {return document. getElementById (id );}
/** The main functionality of splitter **/
Var Splitter = {
Container: null,
LPanel: null,
RPanel: null,
Bar: null,
MovingBar: null,
// Initial left panel, maximum, minimum width
LPanelInitWidth: '250px ',
LPanelMaxWidth: '500px ',
LPanelMinWidth: '200px ',
RPanelInitWidth: '800px ',
RPanelMaxWidth: '999px ',
RPanelMinWidth: '500px ',
// The Color of the separator when it is dragged.
BarActiveColor: '# 0080ff ',
// Set the Maximum/minimum width for the left panel
IsWidthLimit: true,
Init: function (config ){
If (! Config. id ){
Alert ('can not initialize splitter .');
Return;
}
If ($ (config. id )){
This. container = $ (config. id );
If (! ($ ('Splitter _ left_panel ') & $ ('splitter _ right_panel') & $ ('splitter _ bar '))){
Alert ('can not initialize splitter .');
Return;
}
Else {
This. lPanel = $ ('splitter _ left_panel ');
This. rPanel = $ ('splitter _ right_panel ');
This. bar = $ ('itter _ bar ');
}
}
If (config. lPanelMaxWidth ){
This. lPanelMaxWidth = config. lPanelMaxWidth;
}
If (config. lPanelMinWidth ){
This. lPanelMinWidth = config. lPanelMinWidth;
}
If (config. rPanelMaxWidth ){
This. rPanelMaxWidth = config. rPanelMaxWidth;
}
If (config. rPanelMinWidth ){
This. rPanelMinWidth = config. rPanelMinWidth;
}
If (config. lPanelInitWidth ){
This. lPanelInitWidth = config. lPanelInitWidth;
}
If (config. rPanelInitWidth ){
This. rPanelInitWidth = config. rPanelInitWidth;
}
If (config. barActiveColor ){
This. barActiveColor = config. barActiveColor;
}
// Alert (typeof (config. isWidthLimit ));
If (config. isWidthLimit! = Undefined ){
This. isWidthLimit = config. isWidthLimit;
}
Var mask = document. createElement ('P ');
Document. body. appendChild (mask );
With (mask. style ){
Position = 'absolute ';
Left = '0px ';
Top = '0px ';
ZIndex = 900;
Width = '000000 ';
Height = '20180101 ';
Display = 'none ';
BackgroundColor = '# ccc ';
Filter = 'Alpha (opacity = 10 )';
}
// Background-color: red; filter: alpha (opacity = 60)
Splitter. mask = mask;
This. bar. onmousedown = Splitter. start;
},
Start: function (){
Var o = Splitter. container;
O. lastMouseX = event. x;
Splitter. mask. style. display = '';
Var movingBar = document. createElement ('P ');
Splitter. container. appendChild (movingBar );
With (movingBar. style ){
Position = 'absolute ';
Left = Splitter. bar. offsetLeft + 'px ';
Top = '0px ';
Width = Splitter. bar. currentStyle. width;
Height = '20180101 ';
BackgroundColor = Splitter. barActiveColor;
ZIndex = 999;
Cursor = 'col-resize ';
}
MovingBar. dx = 0;
Splitter. movingBar = movingBar;
Document. onmousemove = Splitter. move;
Document. onmouseup = Splitter. end;
},
Move: function (){
Var o = Splitter. container;
Var dx = event. x-o. lastMouseX;
Splitter. movingBar. dx = Splitter. movingBar. dx + dx;
Var left = parseInt (Splitter. movingBar. style. left) + dx;
Splitter. movingBar. style. left = left;
O. lastMouseX = event. x;
},
End: function (){
Document. onmousemove = null;
Document. onmouseup = null;
Splitter. mask. style. display = 'none ';
Var dx = Splitter. movingBar. dx;
Splitter. container. removeChild (Splitter. movingBar );
Var w = parseInt (Splitter. lPanel. currentStyle. width) + dx;
If (Splitter. isWidthLimit ){
Var _ width = (w> parseInt (Splitter. lPanelMaxWidth )? Splitter. lPanelMaxWidth: (w <parseInt (Splitter. lPanelMinWidth )?
Splitter. lPanelMinWidth: w ));
W = _ width;
}
Splitter. lPanel. style. width = w;
}
};
Script
Right-click here to view the source code and save js as splitter. js.
How to Use splitter. js:
The page requires a p as the container (id = splitter_container): You can drag the effect in this container.
The container must contain three p columns (id = splitter_left_panel), split line (id = splitter_bar), and right column (id = splitter_right_panel)
The four p must be modified with css.
#splitter_container{
width: 100%;
height: 100%;
border: solid #eee 1px;
margin: 0px;
padding: 0px;
overflow: hidden;
}
#splitter_left_panel{
width: 300px;
height: 100%;
float: left;
border: solid blue 0px;
}
#splitter_bar{
width: 8px;
height: 100%;
float: left;
background-color: #ccc;
cursor: col-resize;
}
#splitter_right_panel{
height: 100%;
padding-top: 10px;
}
Add an onload event handler to the body to trigger the splitter:
Onload = "Splitter. init ({id: 'splitter _ iner ', isWidthLimit: true });"
The init method of Splitter imports a json object as the configuration parameter, where the container id is required.
You can also configure more parameters, such:
IsWidthLimit: Optional values: true and false. Specify whether to restrict the width of the left panel;
LPanelMaxWidth: maximum width of the left panel, for example, 500px;
LPanelMinWidth: minimum width of the left panel, for example, 100px;
BarActiveColor: The color when the split line is dragged, for example, 'red', '# 0080ff ';
For more information about web development, see blog.csdn.net/sunxing007.