帮助中心
获取牛币
联系我们
关于我们
版权声明
素材牛首页
微博登录
QQ登录
微信扫码登录
注册
登录
新浪微博
QQ登陆
微信登录
热门搜索:
小程序
支付
Java
后台模板
上传
商城模板
jQuery
手机
Thinkphp
微信
Vue.js
首 页
网页特效
整站源码
PHP实例
网站模版
工具箱
常用代码
论坛
游戏源码
资源分享
当前位置:
首页
»
精选常用代码
» JS实现鼠标拖动调整DIV尺寸大小
JS实现鼠标拖动调整DIV尺寸大小
浏览:1074次
-
评论:0次
-
发布时间:2023-08-26
Html
Css
Js
header
content
drag
* { margin:0; padding:0; } #box { width:600px; height:600px; border:1px solid #333; overflow:hidden; } #zhezhao { width:100%; height:100%; background:#f00; filter:alpha(opacity:0); opacity:0; z-index:9999; position:absolute; top:0; left:0; display:none; } #div2 { width:200px; height:200px; position:relative; background:#EEEEEE; border:1px solid #f00; } #div1 { width:15px; height:15px; background:#99CC00; position:absolute; right:0px; bottom:0px; cursor:nw-resize; overflow:hidden; font-size:12px; text-align:center; line-height:15px; color:#FFFFFF; float:right; z-index:3; } #right { width:15px; height:100%; background:#f00; float:right; position:absolute; right:0; top:0; cursor:e-resize; overflow:hidden; filter:alpha(opacity:0); opacity:0; z-index:1; } #bottom { width:100%; height:15px; background:#f00; position:absolute; left:0; bottom:0; cursor:n-resize; overflow:hidden; filter:alpha(opacity:0); opacity:0; z-index:1; } #div2 p { padding:10px; line-height:24px; font-size:13px; text-indent:24px; color:#996600; } #div2 h2 { width:100%; height:25px; line-height:25px; font-size:14px; background:#CC9900; color:#FFFFFF; text-indent:15px; cursor:move; overflow:hidden; }
window.onload = function() { var obox = document.getElementById("box"); var oDiv = document.getElementById("div1"); var oDiv2 = document.getElementById("div2"); var zhezhao = document.getElementById("zhezhao"); var h2 = oDiv2.getElementsByTagName("h2")[0]; var right = document.getElementById("right"); var bottom = document.getElementById("bottom"); var mouseStart = {}; var divStart = {}; var rightStart = {}; var bottomStart = {}; //往右拽 right.onmousedown = function(ev) { var oEvent = ev || event; mouseStart.x = oEvent.clientX; mouseStart.y = oEvent.clientY; rightStart.x = right.offsetLeft; if (right.setCapture) { right.onmousemove = doDrag1; right.onmouseup = stopDrag1; right.setCapture(); } else { document.addEventListener("mousemove", doDrag1, true); document.addEventListener("mouseup", stopDrag1, true); } }; function doDrag1(ev) { var oEvent = ev || event; var l = oEvent.clientX - mouseStart.x + rightStart.x; var w = l + oDiv.offsetWidth; if (w < oDiv.offsetWidth) { w = oDiv.offsetWidth; } else if (w > obox.clientWidth - oDiv2.offsetLeft) { w = obox.clientWidth - oDiv2.offsetLeft - 2; } oDiv2.style.width = w + "px"; }; function stopDrag1() { if (right.releaseCapture) { right.onmousemove = null; right.onmouseup = null; right.releaseCapture(); } else { document.removeEventListener("mousemove", doDrag1, true); document.removeEventListener("mouseup", stopDrag1, true); } }; //往下拽 bottom.onmousedown = function(ev) { var oEvent = ev || event; mouseStart.x = oEvent.clientX; mouseStart.y = oEvent.clientY; bottomStart.y = bottom.offsetTop; if (bottom.setCapture) { bottom.onmousemove = doDrag2; bottom.onmouseup = stopDrag2; bottom.setCapture(); } else { document.addEventListener("mousemove", doDrag2, true); document.addEventListener("mouseup", stopDrag2, true); } }; function doDrag2(ev) { var oEvent = ev || event; var t = oEvent.clientY - mouseStart.y + bottomStart.y; var h = t + oDiv.offsetHeight; if (h < oDiv.offsetHeight) { h = oDiv.offsetHeight; } else if (h > obox.clientHeight - oDiv2.offsetTop) { h = obox.clientHeight - oDiv2.offsetTop - 2; } oDiv2.style.height = h + "px"; }; function stopDrag2() { if (bottom.releaseCapture) { bottom.onmousemove = null; bottom.onmouseup = null; bottom.releaseCapture(); } else { document.removeEventListener("mousemove", doDrag2, true); document.removeEventListener("mouseup", stopDrag2, true); } }; //往左右同时拽 oDiv.onmousedown = function(ev) { var oEvent = ev || event; mouseStart.x = oEvent.clientX; mouseStart.y = oEvent.clientY; divStart.x = oDiv.offsetLeft; divStart.y = oDiv.offsetTop; if (oDiv.setCapture) { oDiv.onmousemove = doDrag; oDiv.onmouseup = stopDrag; oDiv.setCapture(); } else { document.addEventListener("mousemove", doDrag, true); document.addEventListener("mouseup", stopDrag, true); } zhezhao.style.display = 'block'; }; function doDrag(ev) { var oEvent = ev || event; var l = oEvent.clientX - mouseStart.x + divStart.x; var t = oEvent.clientY - mouseStart.y + divStart.y; var w = l + oDiv.offsetWidth; var h = t + oDiv.offsetHeight; if (w < oDiv.offsetWidth) { w = oDiv.offsetWidth; } else if (w > obox.clientWidth - oDiv2.offsetLeft) { w = obox.clientWidth - oDiv2.offsetLeft - 2; } if (h < oDiv.offsetHeight) { h = oDiv.offsetHeight; } else if (h > obox.clientHeight - oDiv2.offsetTop) { h = obox.clientHeight - oDiv2.offsetTop - 2; } oDiv2.style.width = w + "px"; oDiv2.style.height = h + "px"; }; function stopDrag() { if (oDiv.releaseCapture) { oDiv.onmousemove = null; oDiv.onmouseup = null; oDiv.releaseCapture(); } else { document.removeEventListener("mousemove", doDrag, true); document.removeEventListener("mouseup", stopDrag, true); } zhezhao.style.display = 'none'; }; //h2完美拖拽 h2.onmousedown = function(ev) { var oEvent = ev || event; mouseStart.x = oEvent.clientX; mouseStart.y = oEvent.clientY; divStart.x = oDiv2.offsetLeft; divStart.y = oDiv2.offsetTop; if (h2.setCapture) { h2.onmousemove = doDrag3; h2.onmouseup = stopDrag3; h2.setCapture(); } else { document.addEventListener("mousemove", doDrag3, true); document.addEventListener("mouseup", stopDrag3, true); } zhezhao.style.display = 'block'; }; function doDrag3(ev) { var oEvent = ev || event; var l = oEvent.clientX - mouseStart.x + divStart.x; var t = oEvent.clientY - mouseStart.y + divStart.y; if (l < 0) { l = 0; } else if (l > obox.clientWidth - oDiv2.offsetWidth) { l = obox.clientWidth - oDiv2.offsetWidth; } if (t < 0) { t = 0; } else if (t > obox.clientHeight - oDiv2.offsetHeight) { t = obox.clientHeight - oDiv2.offsetHeight; } oDiv2.style.left = l + "px"; oDiv2.style.top = t + "px"; }; function stopDrag3() { if (h2.releaseCapture) { h2.onmousemove = null; h2.onmouseup = null; h2.releaseCapture(); } else { document.removeEventListener("mousemove", doDrag3, true); document.removeEventListener("mouseup", stopDrag3, true); } zhezhao.style.display = 'none'; } };
↑上面代码改变,会自动显示代码结果
jQuery调用版本:
1.11.3
<!doctype html><html><head><meta charset="utf-8"><title>JS实现鼠标拖动调整DIV尺寸大小-www.sucainiu.com</title><script src="https://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script><style>
</style></head><body>
<script>
</script>
</body></html>
立即下载
收藏
积分说明:注册即送10牛币,每日签到可获得5牛币,成为VIP会员可永久免牛币下载!
充值积分
充值会员
更多说明»
代码描述:js鼠标拖动调整div大小代码
jQuery显示与隐藏、淡入淡出、透明度、滑动等效果实例
纯CSS实现的多颜色斜线边框效果
讨论这个常用代码(0)
回答他人问题或分享心得会奖励牛币
〒_〒 居然一个评论都没有……
文明上网,理性发言! 😉 阿里云幸运券,
戳我领取
发表评论
提交回复
我的积分余额:
0
已下载次数:
1074
所需牛币:
5
开始下载
牛币获取:
签到、评论、充值
» 在线充值
(10牛币=1元)
成为VIP可永久免积分下载全部常用代码
» 查看详情
VIP
客服
签到
充值