下面示範如何打造一個類似 EasyUI 的頁面布局:透過拖動中間那條藍色的分隔條,即可即時改變左右兩側 div 的寬度。

具體實現代碼如下:
<style type="text/css"> .div { scrollbar-face-color: #DCDCDC; scrollbar-shadow-color: #99BBE8; scrollbar-highlight-color: #FF3300; scrollbar-3dlight-color: #9EBFE8; scrollbar-darkshadow-color: #9EBFE8; scrollbar-track-color: #DFE8F6; scrollbar-arrow-color: #6699FF; } </style> <div style="overflow: auto; width: 325px; height: 500px; float: left; background-color: Yellow" id="movemodule" class="div"></div> <div id="arrowborder" style="float: left; width: 5px; height: 500px; background-color: Blue; cursor: w-resize;"></div> <div id="rightframe" style="width: 660px; height: 500px; float: left; overflow: auto; background-color: Aqua" class="div"></div> <script type="text/javascript"> var isDown = false; var minwidth = 160; var maxwidth = 800; $("#arrowborder").mousedown(function () { this.setCapture(); isDown = true; $("body").css("cursor", "e-resize"); }); $("body").mouseup(function () { this.releaseCapture(); isDown = false; $("body").css("cursor", "default"); }); $("body").mousemove(function (event) { if (isDown) { var _mx = event.clientX; var _poin = $("#arrowborder").offset(); var _w = _mx - _poin.left; var _lw = $("#movemodule").width(); var _rw = $("#rightframe").width(); if ((_lw + _w > minwidth && _w < 0) || (_lw + _w < maxwidth && _w > 0)) { $("#movemodule").width(_lw + _w); $("#rightframe").width(_rw - _w); } else { if (_w > 0) { $("#movemodule").width(maxwidth); $("#rightframe").width(_rw - (maxwidth - _lw)); } else { $("#movemodule").width(minwidth); $("#rightframe").width(_rw + (_lw - minwidth)); } } } }); </script>原理並不複雜:左側 movemodule、中間 arrowborder、右側 rightframe 三個 div 靠浮動並排;滑鼠在分隔條上按下時進入拖動狀態,移動時依滑鼠位移量同步增減左右兩側的寬度,並透過 minwidth 與 maxwidth 限制可拖動的範圍,避免布局被拉得過窄或過寬。