分隔条左右拖动 js 实现

splitter

【原理】:

  1. 定位

分为3个部分:左边部分、分隔条、右边部分,左边部分与分隔条采用绝对定位。

  1. 事件

监听分隔条的 onmousedown 、onmousemove 、onmouseup 事件, onmousedown 记录下分隔条的初始位置,onmousemove 进行计算,onmousedown 的时候取消操作。

  1. 位置计算

见代码注释。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>分隔条左右拖动 splitter</title>
    <style>
        #root {
            position: relative;
            width: 800px;
            height: 300px;
            margin: 20px auto;
        }
        #left {
            width: 200px;
            background: #ffdd40;
            position: absolute;
            top: 0;
            bottom: 0;
        }
        #right {
            margin-left: 220px;
            background: pink;
            height: 100%;
        }
        #line {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 209px;
            width: 4px;
            background: #e7e7e7;
            box-shadow: 0px 0px 8px #ccc;
            cursor: w-resize;
        }
    </style>
</head>

<body>
    <center>左右拖动分隔条改变显示区域宽度<span id="msg"></span></center>
    <div id="root">
        <div id="left">左边部分</div>
        <div id="right">右边部分</div>
        <!-- 分隔条 -->
        <div id="line"></div>
    </div>
</body>
<script>
    // 改变分隔条左右宽度所需常量
    const leftWidth = 200;  // 左边部分宽度
    const rightToLeftGap = 20;  // 右边部分与左边部分的距离
    const lineWidth = 4; // 分隔条宽度
    const splitMinLeft = 209; // 分隔条左边部分最小宽度
    const splitMaxLeft = 408; // 分隔条左边部分最大宽度

    var oRoot = document.getElementById('root'),
        oLeft = document.getElementById('left'),
        oRight = document.getElementById('right'),
        oLine = document.getElementById('line');

    window.onload = function () {
        oLine.onmousedown = handleLineMouseDown;
    };

    // 分隔条操作
    function handleLineMouseDown(e) {
        // 记录下初始位置的值
        let disX = e.clientX;
        oLine.left = oLine.offsetLeft;

        document.onmousemove = function (e) {
            let moveX = e.clientX - disX;   // 鼠标拖动的偏移距离
            let iT = oLine.left + moveX,    // 分隔条相对父级定位的 left 值
                maxT = oRoot.clientWidth - oLine.offsetWidth;

            iT < 0 && (iT = 0);
            iT > maxT && (iT = maxT);

            if (iT <= splitMinLeft || iT >= splitMaxLeft) return false;

            let leftLineGap = splitMinLeft - leftWidth; // 分隔条距左边部分的距离
            let oLeftWidth = iT - leftLineGap;
            let oRightMarginLeft = oLeftWidth + lineWidth + rightToLeftGap;

            oLine.style.left = `${iT}px`;
            oLeft.style.width = `${oLeftWidth}px`;
            oRight.style.marginLeft = `${oRightMarginLeft}px`;
            return false;
        };

        // 鼠标放开的时候取消操作
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
        };
    }
</script>
</html>

☛ Demo

参考代码:如何添加左右div中间动态调整的分割线功能?

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  •   JavaScript 与 HTML 之间的交互是通过事件实现的。   事件,就是文档或浏览器窗口中发生的一些特...
    霜天晓阅读 3,768评论 1赞 11
  • HTML标签解释大全 一、HTML标记 标签:!DOCTYPE 说明:指定了 HTML 文档遵循的文档类型定义(D...
    米塔塔阅读 3,602评论 1赞 41
  • 1 放大镜实例 页面结构:创建左右两个容器,左边容器中添加一张图片背景图,右边容器中添加一张与背景图成比例放大的图...
    果木山阅读 350评论 0赞 0
  • offset家族三大家族和一个事件对象三大家族(offset/scroll/client)事件对象/event ...
    Yuann阅读 1,136评论 0赞 5
  • 嘉油站这么久没更新了 想我没 嘉油站很久没更新了,今天突然很想聊一聊“关系” 很多时候,“关系”是一个很微...
    小小嘉吖阅读 461评论 0赞 0

友情链接更多精彩内容