zrg
2024-12-26 14b0abe22ae4cdfc1177eeff18fb310520ed850d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(function () {
    // 获取相关DOM元素
    let resize = document.getElementById('resize');
    let top = document.getElementById('top');
    let bottom = document.getElementById('bottom');
    let box = document.getElementById('box');
 
    // 鼠标按下事件
    resize.onmousedown = function (e) {
        let startY = e.clientY;
 
        // 记录起始高度
        let startTopHeight = top.offsetHeight;
        let startBottomHeight = bottom.offsetHeight;
 
        document.onmousemove = function (e) {
            let endY = e.clientY;
            let moveLen = startTopHeight + (endY - startY);  // 计算新的顶部高度
 
            // 获取容器的最大高度(排除拖拽条的高度)
            let maxHeight = box.clientHeight - resize.offsetHeight;
 
            // 限制上方区域最小高度
            if (moveLen < 30) moveLen = 30;
 
            // 限制上方区域最大高度
            if (moveLen > maxHeight - 100) moveLen = maxHeight - 100;
 
            // 设置上方区域的高度
            top.style.height = moveLen + 'px';
 
            // 设置下方区域的高度
            bottom.style.height = (maxHeight - moveLen) + 'px';
        };
 
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
            resize.releaseCapture && resize.releaseCapture(); // 释放鼠标捕获
        };
 
        // 设置鼠标捕获
        resize.setCapture && resize.setCapture();
 
        return false; // 阻止默认事件
    };
})();