js实现左侧边栏可拖动改变显示区域宽度
2017-07-13 13:56
1181 查看
最近有这样的需求,需要实现左侧边栏能够拖动来改变显示的宽度。在网上发现的一个纯js的demo,觉得还不错,由于页面布局不一样,改动有点大,还没有应用到项目中。
先收藏起来,后期用得上,js用起来很方便。
附上demo 源码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
ul,li{margin:0;padding:0;}
body{font:14px/1.5 Arial;color:#666;}
#box{position:relative;width:600px;height:400px;border:2px solid #000;margin:10px auto;overflow:hidden;}
#box ul{list-style-position:inside;margin:10px;}
#top,#bottom{color:#FFF;width:300px;height:400px;overflow:hidden;}
#top{background:green; float:left}
#bottom{background:skyblue;float:right}
#line{position:absolute;top:0;left:50%;height:100%;width:4px;overflow:hidden;background:red;cursor:w-resize;}
</style>
<script>
function $(id) {
return document.getElementById(id)
}
window.onload = function() {
var oBox = $("box"), oTop = $("top"), oBottom = $("bottom"), oLine = $("line");
oLine.onmousedown = function(e) {
var disX = (e || event).clientX;
oLine.left = oLine.offsetLeft;
document.onmousemove = function(e) {
var iT = oLine.left + ((e || event).clientX - disX);
var e=e||window.event,tarnameb=e.target||e.srcElement;
var maxT = oBox.clientWight - oLine.offsetWidth;
oLine.style.margin = 0;
iT < 0 && (iT = 0);
iT > maxT && (iT = maxT);
oLine.style.left = oTop.style.width = iT + "px";
oBottom.style.width = oBox.clientWidth - iT + "px";
$("msg").innerText='top.width:'+oLine.style.width+'---bottom.width:'+oBottom.style.width+'---oLine.offsetLeft:'+oLine.offsetLeft+'---disX:'+disX+'---tarnameb:'+tarnameb.tagName;
return false
};
document.onmouseup = function() {
document.onmousemove = null;
document.onmouseup = null;
oLine.releaseCapture && oLine.releaseCapture()
};
oLine.setCapture && oLine.setCapture();
return false
};
};
</script>
</head>
<body>
<center>左右拖动红条可改变显示区域宽度<span id="msg"></span></center>
<div id="box">
<div id="top">
左
</div>
<div id="bottom">
右
</div>
<div id="line"></div>
</div>
</body>
</html>
先收藏起来,后期用得上,js用起来很方便。
附上demo 源码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>demo</title>
<style>
ul,li{margin:0;padding:0;}
body{font:14px/1.5 Arial;color:#666;}
#box{position:relative;width:600px;height:400px;border:2px solid #000;margin:10px auto;overflow:hidden;}
#box ul{list-style-position:inside;margin:10px;}
#top,#bottom{color:#FFF;width:300px;height:400px;overflow:hidden;}
#top{background:green; float:left}
#bottom{background:skyblue;float:right}
#line{position:absolute;top:0;left:50%;height:100%;width:4px;overflow:hidden;background:red;cursor:w-resize;}
</style>
<script>
function $(id) {
return document.getElementById(id)
}
window.onload = function() {
var oBox = $("box"), oTop = $("top"), oBottom = $("bottom"), oLine = $("line");
oLine.onmousedown = function(e) {
var disX = (e || event).clientX;
oLine.left = oLine.offsetLeft;
document.onmousemove = function(e) {
var iT = oLine.left + ((e || event).clientX - disX);
var e=e||window.event,tarnameb=e.target||e.srcElement;
var maxT = oBox.clientWight - oLine.offsetWidth;
oLine.style.margin = 0;
iT < 0 && (iT = 0);
iT > maxT && (iT = maxT);
oLine.style.left = oTop.style.width = iT + "px";
oBottom.style.width = oBox.clientWidth - iT + "px";
$("msg").innerText='top.width:'+oLine.style.width+'---bottom.width:'+oBottom.style.width+'---oLine.offsetLeft:'+oLine.offsetLeft+'---disX:'+disX+'---tarnameb:'+tarnameb.tagName;
return false
};
document.onmouseup = function() {
document.onmousemove = null;
document.onmouseup = null;
oLine.releaseCapture && oLine.releaseCapture()
};
oLine.setCapture && oLine.setCapture();
return false
};
};
</script>
</head>
<body>
<center>左右拖动红条可改变显示区域宽度<span id="msg"></span></center>
<div id="box">
<div id="top">
左
</div>
<div id="bottom">
右
</div>
<div id="line"></div>
</div>
</body>
</html>
相关文章推荐
- JS实现左右拖动改变内容显示区域大小的方法
- JS实现左右拖动改变内容显示区域大小的方法
- 可拖动拉伸的左右分栏效果,本例子是用Table以及JS来实现的,左右分栏可改变,拖动中间的边框就可实现宽度的调整,点击时可设置为隐藏左侧列表。
- 可拖动拉伸的左右分栏效果,本例子是用Table以及JS来实现的,左右分栏可改变,拖动中间的边框就可实现宽度的调整,点击时可设置为隐藏左侧列表。
- js实现table用鼠标改变td的宽度,固定table宽度和高度超过显示点
- [转] js 实现table每列可左右拖动改变列宽度
- JS 实现 ResizeBar,可拖动改变两个区域(带iframe)大小
- js 实现table每列可左右拖动改变列宽度
- js实现拖动改变层的大小(宽度)
- JS 实现 ResizeBar,可拖动改变两个区域(带iframe)大小
- 如何用JS实现一个DropDownList的值改变时触发另一个DropDownList的显示与隐藏
- Js实现窗口的的显示和拖动
- 通过js实现得到焦点时文本框清空,失去焦点时又显示默认文字,值发生改变时不再恢复默认文字?
- JS实现显示倒影的图片展示特效之改变图片显示大小
- js实现完美拖拽效果可拖动层与回放拖动规迹并显示拖动距离参数
- 鼠标拖动动态改变表格的宽度的js脚本 兼容ie/firefox
- 点滴积累【JS】---JS小功能(JS实现隐藏显示侧边栏,也就是分享栏的隐藏显示)
- js/jquery获取浏览器窗口可视区域高度和宽度以及滚动条高度实现代码
- JS实现当前编辑行自动滚动至div显示区域
- js实现完美拖拽效果可拖动层与回放拖动规迹并显示拖动距离参数