javascript - 无需jQuery UI即可在分隔符的拖放上调整Div大小
问题描述
我正在尝试实现分隔符,它应该能够在拖放时调整 div 的大小。我已经尝试过,但我的代码没有按预期工作。下面是我的代码
HTML
<html style="overflow-y: hidden!important;" >
<head>
<meta name="description" content="Separator">
</head>
<body >
<div id="container" >
<div id="div1" >As of 2016 the WHO website on environmental health states "Environmental health addresses all the physical, chemical, and biological factors external to a person, and all the related factors impacting behaviours. It encompasses the assessment and control of those environmental factors that can potentially affect health. It is targeted towards preventing disease and creating health-supportive environments. This definition excludes behaviour not related to environment, as well as behaviour related to the social and cultural environment, as well as genetics."[2]
The WHO has also defined environmental health services as "those services which implement environmental health policies through monitoring and control activities. They also carry out that role by promoting the improvement of environmental parameters and by encouraging the use of environmentally friendly and healthy technologies and behaviors. They also have a leading role in developing and suggesting new policy areas
</div>
<div class="separator"></div>
<div id="div2" >As of 2016 the WHO website on environmental health states "Environmental health addresses all the physical, chemical, and biological factors external to a person, and all the related factors impacting behaviours. It encompasses the assessment and control of those environmental factors that can potentially affect health. It is targeted towards preventing disease and creating health-supportive environments. This definition excludes behaviour not related to environment, as well as behaviour related to the social and cultural environment, as well as genetics."[2]
The WHO has also defined environmental health services as "those services which implement environmental health policies through monitoring and control activities. They also carry out that role by promoting the improvement of environmental parameters and by encouraging the use of environmentally friendly and healthy technologies and behaviors. They also have a leading role in developing and suggesting new policy areas</div>
</div>
</body>
</html>
Javascript
var oSeparator = document.querySelector(".separator"),
oPrevEl = oSeparator.previousElementSibling,
oNextEl = oSeparator.nextElementSibling;
var iIniSepTop;
var iIniNextElHeight;
var bDrag = false;
function dragStart(oEvent) {
if(oEvent.target == oSeparator) {
bDrag = true;
iIniSepTop = oEvent.clientX - oPrevEl.offsetTop;
iIniNextElHeight = oNextEl.style.height;
}
}
function dragEnd(oEvent) {
bDrag = false;
}
function drag(oEvent) {
if(bDrag){
oEvent.preventDefault();
var iXDiff = iIniSepTop - oEvent.clientX;
oPrevEl.style.height = oEvent.clientX - oPrevEl.offsetTop;
oNextEl.style.height = iIniNextElHeight + iXDiff;
}
}
oSeparator.addEventListener("mousedown", dragStart, false);
oSeparator.addEventListener("mouseup", dragEnd, false);
oSeparator.addEventListener("mousemove", drag, false);
CSS
html,
body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
#container {
height: 100%;
}
.separator {
height: 5px;
background-color: grey;
cursor: n-resize;
position: relative;
}
.separator:hover {
background-color: #5d5c5c;
}
#div1,#div2 {
width: 100%;
overflow-x: hidden;
overflow-y: auto !important;
position: relative;
height: 50%;
}
请不要建议使用 jQuery UI 或任何其他插件,因为我一直在使用相同的插件,我不想使用它来减少整体代码大小,而且我不认为仅将其用于这一功能整个应用程序。
它没有按预期工作(Div 的高度应该调整大小并根据分隔符拖放时页面的相对高度进行调整)。
解决方案
推荐阅读
- php - 自定义运输选择选项
- android - 如果使用 Google Play 服务,则无法使用最新的 compileSdkVersion 和 targetSdkVersion 而不会发生版本冲突
- c# - 如何找出两个圆圈是否重叠或统一?
- django - 如何通过 MultipleChoiceField 保留序列化 JSON 的顺序
- javascript - javascript过滤方法查询
- css - 溢出-x:滚动不正确滚动
- tfs - 在远程环境 TFS2018 上发布
- selenium - 如何在不使用 initElements() 或 testng 类中的 new Operator 的情况下访问 Page Class 方法?
- excel - 指向现有工作表 Vba 的超链接
- node.js - 带有 Express 和 Mongoose 的 NodeJS 试图从另一个中间件调用一个中间件