拖动改变上下两个div大小

本文介绍了一个使用HTML、CSS及JavaScript实现的拖拽效果案例,通过拖动界面上的分割线来调整两个区域的高度比例。适用于网站布局或应用程序中需要灵活调整大小的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!DOCTYPE html>  
<html>  
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />  
<title>drag div</title> 
<style type="text/css">  
<style type="text/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;}  
#box ul{list-style-position:inside;margin:10px;}  
#box div{position:absolute;width:100%;}  
#top,#bottom{color:#FFF;height:100%;overflow:hidden;}  
#bottom{background-color:white;top:50%}  
#line{top:50%;height:4px;overflow:hidden;margin-top:-2px;background:red;cursor:n-resize;}
</style>
<script>  
function $(id) {  
return document.getElementById(id)  
}  
window.onload = function() {  
var oBox = $("box"), oBottom = $("bottom"), oLine = $("line");  
oLine.onmousedown = function(e) {  
var disY = (e || event).clientY;  
oLine.top = oLine.offsetTop;  
document.onmousemove = function(e) {  
var iT = oLine.top + ((e || event).clientY - disY);  
var maxT = oBox.clientHeight - oLine.offsetHeight;  
oLine.style.margin = 0;  
iT < 0 && (iT = 0);  
iT > maxT && (iT = maxT);  
oLine.style.top = oBottom.style.top = iT + "px";  
return false  
};  
document.onmouseup = function() {  
document.onmousemove = null;  
document.onmouseup = null;  
oLine.releaseCapture && oLine.releaseCapture()  
};  
oLine.setCapture && oLine.setCapture();  
return false  
};  
};  
</script>
<div id="box">  
<div id="top">
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
</div>  
<div id="bottom">
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
<font color='black'>2</font></br>
</div>  
<div id="line"></div>  
</div>  
</body>  
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值