js 分栏效果实现代码_javascript技巧
网上我也见到一些分栏效果,也有一个jquery的插件jquery.splitter.js, 但是他们基本都没有解决一个问题:如果页面上有iframe, 当拖动分割线经过iframe的时候,鼠标不听使唤了,我曾经开过帖子讨论过这个问题。本例采用一个小技巧解决了这个问题,使拖动流畅。
<script> <BR>/* <BR>* splitter.js <BR>* author: sunxing007 <BR>* http://blog.csdn.net/sunxing007 <BR>* date: 08/26/2009 <br><br>************************************************************************************** <BR>The css script below is needed for the html page when using splitter.js, please save <BR>it as splitter.css, and modify it carefully. <BR>************************************************************************************** <BR>#splitter_container{ <BR>width: 100%; <BR>height: 100%; <BR>border: solid #eee 1px; <BR>margin: 0px; <BR>padding: 0px; <BR>overflow: hidden; <BR>} <BR>#splitter_left_panel{ <BR>width: 300px; <BR>height: 100%; <BR>float: left; <BR>border: solid blue 0px; <BR>} <BR>#splitter_bar{ <BR>width: 8px; <BR>height: 100%; <BR>float: left; <BR>background-color: #ccc; <BR>cursor: col-resize; <BR>} <BR>#splitter_right_panel{ <BR>height: 100%; <BR>padding-top: 10px; <BR>} <BR>************************************************************************************** <BR>How to use this splitter? <BR>************************************************************************************** <BR><!-- <BR> <html> <BR><head> <BR><title>Splitter demo <BR><link href="splitter.css" type="text/css" rel="stylesheet" /> <BR><script src="splitter.js"></script>
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修饰一下
<br>#splitter_container{ <br>width: 100%; <br>height: 100%; <br>border: solid #eee 1px; <br>margin: 0px; <br>padding: 0px; <br>overflow: hidden; <br>}<br> <br>#splitter_left_panel{ <br>width: 300px; <br>height: 100%; <br>float: left; <br>border: solid blue 0px; <br>}<br> <br>#splitter_bar{ <br>width: 8px; <br>height: 100%; <br>float: left; <br>background-color: #ccc; <br>cursor: col-resize; <br>}<br> <br>#splitter_right_panel{ <br>height: 100%; <br>padding-top: 10px; <br>} <br>
给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

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

因此,在这里,您准备好了解所有称为Ajax的东西。但是,到底是什么? AJAX一词是指用于创建动态,交互式Web内容的一系列宽松的技术。 Ajax一词,最初由Jesse J创造

10款趣味横生的jQuery游戏插件,让您的网站更具吸引力,提升用户粘性!虽然Flash仍然是开发休闲网页游戏的最佳软件,但jQuery也能创造出令人惊喜的效果,虽然无法与纯动作Flash游戏媲美,但在某些情况下,您也能在浏览器中获得意想不到的乐趣。 jQuery井字棋游戏 游戏编程的“Hello world”,现在有了jQuery版本。 源码 jQuery疯狂填词游戏 这是一个填空游戏,由于不知道单词的上下文,可能会产生一些古怪的结果。 源码 jQuery扫雷游戏

本教程演示了如何使用jQuery创建迷人的视差背景效果。 我们将构建一个带有分层图像的标题横幅,从而创造出令人惊叹的视觉深度。 更新的插件可与JQuery 1.6.4及更高版本一起使用。 下载

本文讨论了在浏览器中优化JavaScript性能的策略,重点是减少执行时间并最大程度地减少对页面负载速度的影响。

Matter.js是一个用JavaScript编写的2D刚体物理引擎。此库可以帮助您轻松地在浏览器中模拟2D物理。它提供了许多功能,例如创建刚体并为其分配质量、面积或密度等物理属性的能力。您还可以模拟不同类型的碰撞和力,例如重力摩擦力。 Matter.js支持所有主流浏览器。此外,它也适用于移动设备,因为它可以检测触摸并具有响应能力。所有这些功能都使其值得您投入时间学习如何使用该引擎,因为这样您就可以轻松创建基于物理的2D游戏或模拟。在本教程中,我将介绍此库的基础知识,包括其安装和用法,并提供一

本文演示了如何使用jQuery和ajax自动每5秒自动刷新DIV的内容。 该示例从RSS提要中获取并显示了最新的博客文章以及最后的刷新时间戳。 加载图像是选择
