javascript - 如何使用纯 JAvaScript 制作 div resizer
问题描述
我正在尝试创建一个 div resizer,由于一些限制,我不能使用 jQuery,我不得不为此使用纯 JavaScript。在当前状态下,它可以工作,但是如果将具有滑块的 div 没有将位置设置为绝对位置,它就会中断。有没有办法解决这个问题?非常感谢。
我是一名学习编写 JavaScript/CSS/HTML 代码的学生,所以我对此比较陌生。
const BORDER_SIZE = 4;
const panel = document.getElementById("left_panel");
const StationaryPanel = document.getElementById("right_panel");
const parent = document.getElementById("parent");
const label1 = document.getElementById("lb1");
const label2 = document.getElementById("lb2");
const label3 = document.getElementById("lb3");
let m_pos;
function resize(e) {
const dx = m_pos - e.x;
m_pos = e.x;
lb1.innerHTML = panel.offsetWidth;
lb2.innerHTML = StationaryPanel.offsetWidth;
lb3.innerHTML = parent.offsetWidth;
//lb3.innerHTML = document.body.clientWidth;
panel.style.width = (parseInt(getComputedStyle(panel, '').width) + dx) + "px";
StationaryPanel.style.width = (parent.offsetWidth - panel.offsetWidth) + "px";
//StationaryPanel.style.width = (document.body.clientWidth - panel.offsetWidth) + "px";
}
panel.addEventListener("mousedown", function(e) {
if (e.offsetX < BORDER_SIZE) {
m_pos = e.x;
if (panel.style.width < panel.minWidth || panel.style.width > panel.maxWidth) {
return;
}
document.addEventListener("mousemove", resize, false);
}
}, false);
document.addEventListener("mouseup", function() {
document.removeEventListener("mousemove", resize, false);
}, false);
#left_panel {
position: absolute;
width: 96px;
min-width: 50px;
max-width: 500px;
padding-left: 4px;
height: 100%;
right: 0;
background-color: #f0f0ff;
}
#left_panel::before {
content: " ";
background-color: #ccc;
position: absolute;
left: 0;
width: 4px;
height: 100%;
cursor: w-resize;
}
#right_panel {
width: 1000px;
height: 100%;
background-color: #ff0000;
}
#parent {
width: 800px;
}
<body>
<div id="parent">
<div id="left_panel">
This is the left div, the one that moves
</div>
<div id="right_panel">
This is the right div, the one that stays the same
</div>
</div>
<p id="lb1"></p>
<p>This is the left panel width ^</p>
<p id="lb2"></p>
<p>This is the right panel width ^</p>
<p id="lb3"></p>
<p>This is the parent width ^</p>
</body>
解决方案
如果这不是问题,这是您可以使用的最简单的方法CSS
,这里不需要使用 JS 来实现此功能,这只是一个示例,但它绝对可以帮助您。
#MainDiv {
display: flex;
flex-direction: row;
height: 200px;
width: 400px;
border: 1px solid red;
position: relative;
}
#leftDiv {
width: 200px;
border: 1px solid yellow;
}
#rightDiv {
border: 2px solid;
padding: 20px;
width: 300px;
/* you can make this vertical/auto to make resize both ways */
resize: horizontal;
overflow: auto;
}
<div id="MainDiv">
<div id="leftDiv">
Left Div
</div>
<div id="rightDiv">
Right Div
</div>
</div>
这是Javascript中的:
var h = $('#handle'),
l = $('#left'),
r = $('#right'),
w = $('body').width() - 18;
var isDragging = false;
h.mousedown(function(e) {
isDragging = true;
e.preventDefault();
});
$(document).mouseup(function() {
isDragging = false;
}).mousemove(function(e) {
if (isDragging) {
l.css('width', e.pageX);
r.css('width', w - e.pageX);
}
});
#left,
#right {
border: 1px solid #aaa;
float: left;
height: 100px;
width: 48%;
}
#handle {
background: #000;
float: left;
height: 100px;
margin: 1px;
/* Slider width */
width: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="left"> Left</div>
<div id="handle"></div>
<div id="right">Right</div>
推荐阅读
- ubuntu - Ubuntu 桌面应用程序会在 Ubuntu Touch OS 和使用 Electron 的设备上运行吗?
- monitoring - 有效监控?如何避免交替疲劳?
- python - 如何使用 python 在 dialogflow CX 中传递查询参数
- c# - RadGridView 子行中的用户控件 - Telerik Winforms
- python-3.x - 是否可以使用 api 将数据框保存到 atlassian 页面?
- python - 在数据中查找最大值和最小值并将 True/False 附加到相应的行 - Python
- r - 如何在闪亮的应用程序中使用 renv 包以避免在闪亮的服务器上安装新包?
- javascript - 在 Javascript 中,如何在再次触发后停止触发的声音?
- python - 根据相应列值的聚合有条件地填充 NA
- amazon-web-services - Terraform - 超过 20k 个字符的 Cognito 邀请消息