<!DOCTYPE html>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
</head>
<body>
<div id="weiMain">
<div id="weiLeft">
左侧边栏区域
</div>
<div id="weiLeftResize" title="收缩markdown侧边栏">
<button onclick="leftDragControllerDivVisiable();return false;" class="weiresizeBtn">=</button>
</div>
<div id="weiContent">
<div id="weiMarkdownContent">内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域</div>
</div>
<div id="weiRightResize" title="收缩markdown侧边栏">
<button onclick="rightDragControllerDivVisiable();return false;" class="weiresizeBtn weiresizeBtnRight">= </button>
</div>
<div id="weiRight">
右侧边栏区域
</div>
</div>
</body>
</html>
<style>
* {
margin: 0;
padding: 0;
}
:root {
--leftWidth: 15%;
--leftBorderRightColor: red;
--leftResizeWidth: 10px;
--leftResizeBgHover: aqua;
--rightWidth: 15%;
--rightBorderRightColor: red;
--rightResizeWidth: 10px;
--rightResizeBgHover: aqua;
}
#weiMain {
position: fixed;
width: 100%;
height: 100%;
left: 0;
top: 0;
}
#weiMain>div {
position: absolute;
}
#weiLeft {
width: var(--leftWidth);
height: 100%;
left: 0;
top: 0;
border-right: 1px solid var(--leftBorderRightColor);
box-sizing: border-box;
}
#weiRight {
width: var(--rightWidth);
height: 100%;
right: 0;
top: 0;
border-left: 1px solid var(--rightBorderRightColor);
box-sizing: border-box;
}
#weiLeftResize {
width: var(--leftResizeWidth);
height: 100%;
left: var(--leftWidth);
top: 0;
z-index: 999;
}
#weiRightResize {
width: var(--rightResizeWidth);
height: 100%;
right: var(--rightWidth);
top: 0;
z-index: 999;
}
#weiLeftResize:hover,
#weiRightResize:hover {
background-color: var(--leftResizeBgHover);
cursor: col-resize;
}
#weiContent {
width: 100%;
height: 100%;
padding-left: calc(var(--leftWidth) + var(--leftResizeWidth));
padding-right: calc(var(--rightWidth) + var(--rightResizeWidth));
z-index: -99;
box-sizing: border-box;
}
.weiresizeBtn {
position: absolute;
bottom: 0;
box-sizing: 99999;
width: 20px;
height: 20px;
}
.weiresizeBtnRight{
right: 0;
}
</style>
<script>
function leftDragControllerDiv(main = "#weiMain", sidebar = "#weiLeft", resize = "#weiLeftResize", content = "#weiContent", minleft = 0, minright = 0) {
var resize = document.querySelector(resize);
var left = document.querySelector(sidebar);
var mid = document.querySelector(content);
var box = document.querySelector(main);
resize.onmousedown = function (e) {
var startX = e.clientX;
resize.left = resize.offsetLeft;
document.onmousemove = function (e) {
var endX = e.clientX;
var moveLen = resize.left + (endX - startX);
var maxT = box.clientWidth - resize.offsetWidth;
if (moveLen < minleft) moveLen = minleft;
if (moveLen > maxT - minright) moveLen = maxT - minright;
console.log(moveLen);
left.style.width = moveLen + 'px';
resize.style.left = moveLen + 'px';
mid.style.paddingLeft = (moveLen + resize.offsetWidth) + 'px';
};
document.onmouseup = function (evt) {
document.onmousemove = null;
document.onmouseup = null;
resize.releaseCapture && resize.releaseCapture();
};
resize.setCapture && resize.setCapture();
return false;
};
}
function leftDragControllerDivVisiable(main = "#weiMain", sidebar = "#weiLeft", resize = "#weiLeftResize", content = "#weiContent", minleft = 0, minright = 0) {
var resize = document.querySelector(resize);
var left = document.querySelector(sidebar);
var mid = document.querySelector(content);
var box = document.querySelector(main);
if (resize.left <= minleft) {
console.log(1);
const root = document.documentElement;
const leftWidth = window.getComputedStyle(root).getPropertyValue('--leftWidth');
left.style.width = leftWidth;
resize.style.left = leftWidth;
mid.style.paddingLeft = leftWidth;
} else {
left.style.width = 0;
resize.style.left = 0;
mid.style.paddingLeft = (0 + resize.offsetWidth) + 'px';
}
}
function rightDragControllerDiv(main = "#weiMain", sidebar = "#weiRight", resize = "#weiRightResize", content = "#weiContent", minleft = 0, minright = 0) {
var right = document.querySelector(sidebar);
var mid = document.querySelector(content);
var box = document.querySelector(main);
var resize = document.querySelector(resize);
resize.onmousedown = function (e) {
var startX = e.clientX;
resize.left = resize.offsetLeft;
document.onmousemove = function (e) {
var endX = e.clientX;
var moveLen = resize.left + (endX - startX);
var maxT = box.clientWidth - resize.offsetWidth;
if (moveLen < minleft) moveLen = minleft;
if (moveLen > maxT - minright) moveLen = maxT - minright;
resize.style.right = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px';
right.style.width = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px';
mid.style.paddingRight = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px';
};
document.onmouseup = function (evt) {
document.onmousemove = null;
document.onmouseup = null;
resize.releaseCapture && resize.releaseCapture();
};
resize.setCapture && resize.setCapture();
return false;
};
}
function rightDragControllerDivVisiable(main = "#weiMain", sidebar = "#weiRight", resize = "#weiRightResize", content = "#weiContent", minleft = 0, minright = 0) {
var right = document.querySelector(sidebar);
var mid = document.querySelector(content);
var box = document.querySelector(main);
var resize = document.querySelector(resize);
if (mid.style.paddingRight == minright + 'px') {
const root = document.documentElement;
const rightWidth = window.getComputedStyle(root).getPropertyValue('--rightWidth');
var maxT = box.clientWidth - rightWidth
console.log( box.clientWidth , rightWidth);
resize.style.right = right.style.width = mid.style.paddingRight =rightWidth ;
} else {
resize.style.right = right.style.width = mid.style.paddingRight =0 ;
}
}
leftDragControllerDiv();
rightDragControllerDiv();
</script>
<style>
</style>
文章评论