网上我也见到一些分栏效果,也有一个jquery的插件jquery.splitter.js, 但是他们基本都没有解决一个问题:如果页面上有iframe, 当拖动分割线经过iframe的时候,鼠标不听使唤了,我曾经开过帖子讨论过这个问题。本例采用一个小技巧解决了这个问题,使拖动流畅。
复制代码 代码如下:
splitter demo
left panel
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,
//左面板初始,最大,最小宽度
lpanelinitwidth: '250px',
lpanelmaxwidth: '500px',
lpanelminwidth: '200px',
rpanelinitwidth: '800px',
rpanelmaxwidth: '999px',
rpanelminwidth: '500px',
//分隔线被拖动的时候的颜色
baractivecolor: '#0080ff',
//左面的面板是否设置最大/最小宽度
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 = $('splitter_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('div');
document.body.appendchild(mask);
with(mask.style){
position = 'absolute';
left = '0px';
top = '0px';
zindex = 900;
width = '100%';
height = '100%';
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('div');
splitter.container.appendchild(movingbar);
with(movingbar.style){
position = 'absolute';
left = splitter.bar.offsetleft + 'px';
top = '0px';
width = splitter.bar.currentstyle.width;
height = '100%';
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 splitter.lpanelminwidth : w));
w = _width;
}
splitter.lpanel.style.width = w;
}
};
在此处右键察看源代码并把其中的js保存为splitter.js
splitter.js使用方法:
页面上需要有一个div作为容器(id=splitter_container): 可拖动效果就在这个容器里面进行
容器里面需要有3个div,分别代表左栏(id=splitter_left_panel),分割线(id=splitter_bar), 右栏(id=splitter_right_panel)
这4个div需要用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;
}
给body加上onload事件处理函数,以触发splitter:
onload=splitter.init({id: 'splitter_container', iswidthlimit: true});
splitter的init方法传入一个json对象作为配置参数,其中容器id是必需的.
还可以配置更多的参数, 比如:
iswidthlimit: 可选值true, false, 设置左面板是否限制宽度;
lpanelmaxwidth: 左面板最大宽度,比如: 500px;
lpanelminwidth: 左面板最小宽度,比如: 100px;
baractivecolor: 分割线拖动的时候的颜色: 比如'red', '#0080ff';
更多web开发相关的内容就在blog.csdn.net/sunxing007