一、重新计算to_x,to_y设定最小范围限定
提示: 全局变量中已经设定好了最小宽度和最小高度m_min_w和m_min_h.只要使用数学对象的max函数返回最小值和现值哪个更大就可以
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>慕课网-Resizable</title> <style type="text/css"> body{ margin: 0px;padding: 50px;font-size: 14px;color: #333; } .panel{ width: 400px;height: 240px; border:1px solid #ccc;position: relative; } .panel .title{ background-color: #eee;text-align: center;line-height: 30px; border: 1px solid #fff; font-weight: bold; } .ui-Resizable-r{ /* 右侧控制元素样式 */ position: absolute;right: 0px;top: 0px; width: 10px;height: 100%; background-color: green; cursor: e-resize; } .ui-Resizable-b{ /* 底边控制元素样式 */ position: absolute;right: 0px;bottom: 0px; width: 100%;height:10px ; background-color: blue; cursor: s-resize; } .ui-Resizable-rb{ /* 右下角控制元素样式 */ position: absolute;right: 0px;bottom: 0px; width: 20px;height:20px ; background-color: red; cursor: nw-resize; } </style> </head> <body> <div class="panel" id="ui-Resizable"> <div class="title">Resizable Panel</div> </div> <script type="text/javascript"> // ctrl :控制元素,panel :面板 , type 类型 var m_panel , m_ctrl , m_type ; // move: 是否侦听鼠标移动, // m_start_x: 鼠标相对ctrl元素的left、right // m_to_x: 鼠标的新位置 var moving = 0 ,m_start_x = 0 , m_start_y = 0 , m_to_x = 0 , m_to_y = 0 ; // 面板最小尺寸 var m_min_w = 100,m_min_h = 40; // 在控制元素中按下 function on_mousedown( e , panel ,ctrl , type ){ var e = e || window.event; // 计算出鼠标页面位置 和 当前元素位置的差 = 鼠标相对元素的位置 m_start_x = e.pageX - ctrl.offsetLeft; m_start_y = e.pageY - ctrl.offsetTop; m_panel = panel; m_ctrl = ctrl; m_type = type; // 开始处理移动事件 moving = setInterval(on_move,10); } // 页面鼠标移动侦听处理 document.onmousemove = function( e ){ var e = window.event || e; m_to_x = e.pageX; m_to_y = e.pageY; } // 鼠标移动处理 function on_move(){ if(moving){ /* 重新计算to_x,to_y设定最小范围限定 */ } } // 鼠标弹起处理 document.onkeyup = document.onmouseup = function(e){ clearInterval(moving); var cls = document.getElementsByClassName('ui-Resizable-ctrl'); for(var i=0;i<cls.length;i++){ cls[i].style.left = ''; cls[i].style.top = ''; } } function Resizable( panelId ){ var panel = document.getElementById(panelId); // 插入调整控制元素 var r = document.createElement("div"); var b = document.createElement("div"); var rb = document.createElement("div"); r.class = r.className = 'ui-Resizable-r ui-Resizable-ctrl'; b.class = b.className = 'ui-Resizable-b ui-Resizable-ctrl'; rb.class = rb.className = 'ui-Resizable-rb ui-Resizable-ctrl'; panel.appendChild(r); panel.appendChild(b); panel.appendChild(rb); // 为调整控制元素设置拖拽处理 r.addEventListener('mousedown',function(e){ on_mousedown(e,panel,r,'r'); }) b.addEventListener('mousedown',function(e){ on_mousedown(e,panel,b,'b'); }) rb.addEventListener('mousedown',function(e){ on_mousedown(e,panel,rb,'rb'); }) } Resizable('ui-Resizable'); </script> </script> </body> </html>