思路:采用flex布局,js即时修改固定列的宽度

注意:父元素需设置position:relative;因offsetLeft和offsetTop是相对于具有定位的(position:absolute或者position:relative)父级元素的距离

html:

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="./index.css">
<link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/4.7.0/css/font-awesome.css">
<script src="./index.js"></script>
</head> <body>
<div id="box">
<div id="left">
</div>
<div id="line">
<a id="line1">
<span><i class="fa fa-caret-right"></i></span>
</a>
</div>
<div id="right">
</div>
</div>
</body> </html>

css:

body {
font: 14px/1.5 Arial;
color: #666;
} #box {
position: relative;
width: 600px;
height: 400px;
border: 2px solid #000;
margin: 10px auto;
overflow: hidden;
display: flex;
} #left {
height: 100%;
flex:;
overflow: hidden;
} #right {
width: 300px;
overflow: hidden;
} #line {
width: 8px;
background: lightblue;
cursor: col-resize;
} #line a{
cursor: pointer;
text-align: center;
}

js:

function $(id) {
return document.getElementById(id)
}
window.onload = function () {
var oBox = $("box"),
oLeft = $("left"),
oRight = $("right"),
oLine = $("line"),
oLine1 = $("line1");
var flag = true;
var wid = 0; oLine.onmousedown = function (e) {
var disX = (e || event).clientX;
var owidth = oBox.clientWidth - oLine.offsetLeft;
document.onmousemove = function (e) {
var iT = owidth - ((e || event).clientX - disX);
var e = e || window.event;
var maxT = oBox.clientWidth - oLine.offsetWidth;
oLine.style.margin = 0;
iT < 30 && (iT = 30);
iT > maxT && (iT = maxT);
wid = iT;
oRight.style.width = iT + "px";
return false
};
document.onmouseup = function () {
document.onmousemove = null;
document.onmouseup = null;
oLine.releaseCapture && oLine.releaseCapture()
};
oLine.setCapture && oLine.setCapture();
return false
}; oLine1.onclick = function () {
if (flag) {
oRight.style.width = 30 + "px";
flag = false;
} else {
if (wid && wid > 30) {
oRight.style.width = wid + "px";
} else {
oRight.style.width = 300 + "px";
}
flag = true;
}
} };

效果如下:

最新文章

  1. 几款主流PHP框架的优缺点评比
  2. aspx前台调用cs后台方法
  3. 做中学之Vim实践教程
  4. C++-模板的声明和实现为何要放在头文件中
  5. docker加速器
  6. 从零开始学ios开发(十七):Storyboards(上)
  7. 线性布局LinearLayout和相对布局RelativeLayout 之间的比较
  8. sublime text3-代码片段配置
  9. java poi 合并单元格后边框问题
  10. dev gridcontrol 单箱效果
  11. mssql 一次向表中插入多条数据的方法分享 (转自:http://www.maomao365.com/?p=6058)
  12. python数学第九天【统计】
  13. 【XSY1602】安全网络 树形DP 数学
  14. springboot寻找property的顺序
  15. Git 换行符检查 CRLF 与 LF
  16. Https 安全传输的原理
  17. js检测来源网址,如果是搜索引擎跳转到新地址
  18. 【转载】Linux内存中buffer和 cached的比较
  19. MSSQL SEVER执行动态SQL
  20. Java Activiti 工作流引擎 springmvc SSM 流程审批 后台框架源码

热门文章

  1. mysql 分区优缺点
  2. cin关闭流同步加速
  3. 【deep learning精华部分】稀疏自编码提取高阶特征、多层微调完全解释及代码逐行详解
  4. LeetCode OJ:Count Primes(质数计数)
  5. JS兼容性汇总
  6. Spring Boot打包总结
  7. mac 开启SSH服务
  8. 2018下C程序设计(上)第0次作业
  9. &quot;==&quot; 与 &quot;is&quot;的区别
  10. 狗狗有关的知识tips