• 三栏侧边栏的实现


    在这里插入图片描述

    <!DOCTYPE html>
    <html>
    
    <head>
      <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
      <meta name="viewport" content="width=device-width,initial-scale=1">
      <meta charset="UTF-8">
    </head>
    
    <body>
      <div id="weiMain">
        <div id="weiLeft">
          左侧边栏区域
        </div>
        <div id="weiLeftResize" title="收缩markdown侧边栏">
          <button onclick="leftDragControllerDivVisiable();return false;" class="weiresizeBtn">=</button>
        </div>
        <div id="weiContent">
          <div id="weiMarkdownContent">内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域内容区域</div>
        </div>
        <div id="weiRightResize" title="收缩markdown侧边栏">
          <button onclick="rightDragControllerDivVisiable();return false;" class="weiresizeBtn weiresizeBtnRight">= </button>
        </div>
        <div id="weiRight">
          右侧边栏区域
        </div>
      </div>
    </body>
    
    </html>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
    
      :root {
        --leftWidth: 15%;
        --leftBorderRightColor: red;
        --leftResizeWidth: 10px;
        --leftResizeBgHover: aqua;
        --rightWidth: 15%;
        --rightBorderRightColor: red;
        --rightResizeWidth: 10px;
        --rightResizeBgHover: aqua;
      }
    
      #weiMain {
        position: fixed;
        width: 100%;
        height: 100%;
        left: 0;
        top: 0;
      }
    
      #weiMain>div {
        position: absolute;
      }
    
      #weiLeft {
        width: var(--leftWidth);
        height: 100%;
        left: 0;
        top: 0;
        border-right: 1px solid var(--leftBorderRightColor);
        box-sizing: border-box;
      }
    
      #weiRight {
        width: var(--rightWidth);
        height: 100%;
        right: 0;
        top: 0;
        border-left: 1px solid var(--rightBorderRightColor);
        box-sizing: border-box;
      }
    
      #weiLeftResize {
        width: var(--leftResizeWidth);
        height: 100%;
        left: var(--leftWidth);
        top: 0;
        /* background-color: rgb(204, 211, 211); */
        z-index: 999;
      }
    
      #weiRightResize {
        width: var(--rightResizeWidth);
        height: 100%;
        right: var(--rightWidth);
        top: 0;
        /* background-color: rgb(204, 211, 211); */
        z-index: 999;
      }
    
      #weiLeftResize:hover,
      #weiRightResize:hover {
        background-color: var(--leftResizeBgHover);
        cursor: col-resize;
      }
    
      #weiContent {
        width: 100%;
        height: 100%;
        padding-left: calc(var(--leftWidth) + var(--leftResizeWidth));
        padding-right: calc(var(--rightWidth) + var(--rightResizeWidth));
        z-index: -99;
        box-sizing: border-box;
      }
    
      .weiresizeBtn {
        position: absolute;
        bottom: 0;
        box-sizing: 99999;
        width: 20px;
        height: 20px;
      }
      .weiresizeBtnRight{
       right: 0;
      }
    </style>
    <script>
      // 左侧拖拽
      function leftDragControllerDiv(main = "#weiMain", sidebar = "#weiLeft", resize = "#weiLeftResize", content = "#weiContent", minleft = 0, minright = 0) {
        // 层次结构:main 》 sidebar = resize = content
        var resize = document.querySelector(resize);         // 拖动条
        var left = document.querySelector(sidebar);          // 侧边栏
        var mid = document.querySelector(content);           // 内容区域
        var box = document.querySelector(main);              // 整个大区域
        // 鼠标按下事件
        resize.onmousedown = function (e) {
          //颜色改变提醒
          // resize.style.background = '#818181';
          var startX = e.clientX;
          resize.left = resize.offsetLeft;
          // 鼠标拖动事件
          document.onmousemove = function (e) {
            var endX = e.clientX;
            var moveLen = resize.left + (endX - startX); // (endx-startx)=移动的距离。resize.left+移动的距离=左边区域最后的宽度
            var maxT = box.clientWidth - resize.offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度
    
            if (moveLen < minleft) moveLen = minleft; // 左边区域的最小宽度为32px
            if (moveLen > maxT - minright) moveLen = maxT - minright; //右边区域最小宽度为150px
    
    
            console.log(moveLen);
            left.style.width = moveLen + 'px'; // 设置左侧区域的宽度
            resize.style.left = moveLen + 'px'; // 设置拖动条的位置
            mid.style.paddingLeft = (moveLen + resize.offsetWidth) + 'px'; // 注意加上滚动条的宽度
          };
          // 鼠标松开事件
          document.onmouseup = function (evt) {
            //颜色恢复
            // resize.style.background = '#d6d6d6';
            document.onmousemove = null;
            document.onmouseup = null;
            resize.releaseCapture && resize.releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
          };
          resize.setCapture && resize.setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
          return false;
        };
      }
      function leftDragControllerDivVisiable(main = "#weiMain", sidebar = "#weiLeft", resize = "#weiLeftResize", content = "#weiContent", minleft = 0, minright = 0) {
        var resize = document.querySelector(resize);         // 拖动条
        var left = document.querySelector(sidebar);          // 侧边栏
        var mid = document.querySelector(content);           // 内容区域
        var box = document.querySelector(main);              // 整个大区域
        if (resize.left <= minleft) {
          console.log(1);
          const root = document.documentElement;
          const leftWidth = window.getComputedStyle(root).getPropertyValue('--leftWidth');
          left.style.width = leftWidth; // 设置左侧区域的宽度
          resize.style.left = leftWidth; // 设置拖动条的位置
          mid.style.paddingLeft = leftWidth; // 注意加上滚动条的宽度
        } else {
          left.style.width = 0; // 设置左侧区域的宽度
          resize.style.left = 0; // 设置拖动条的位置
          mid.style.paddingLeft = (0 + resize.offsetWidth) + 'px'; // 注意加上滚动条的宽度
        }
      }
      // 右侧拖拽
      function rightDragControllerDiv(main = "#weiMain", sidebar = "#weiRight", resize = "#weiRightResize", content = "#weiContent", minleft = 0, minright = 0) {
        // 层次结构:main 》 sidebar = resize = content
        var right = document.querySelector(sidebar);          // 侧边栏
        var mid = document.querySelector(content);           // 内容区域
        var box = document.querySelector(main);              // 整个大区域
        var resize = document.querySelector(resize);  // 拖动条
    
        // 鼠标按下事件
        resize.onmousedown = function (e) {
          //颜色改变提醒
          // resize.style.background = '#818181';
          var startX = e.clientX;
          resize.left = resize.offsetLeft;
          // 鼠标拖动事件
          document.onmousemove = function (e) {
            var endX = e.clientX;
            var moveLen = resize.left + (endX - startX); // (endx-startx)=移动的距离。resize.left+移动的距离=左边区域最后的宽度
            var maxT = box.clientWidth - resize.offsetWidth; // 容器宽度 - 左边区域的宽度 = 右边区域的宽度
    
            if (moveLen < minleft) moveLen = minleft; // 左边区域的最小宽度为32px
            if (moveLen > maxT - minright) moveLen = maxT - minright; //右边区域最小宽度为150px
    
            resize.style.right = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px'; // 设置拖动条的位置
            right.style.width = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px'; // 设置左侧区域的宽度
            mid.style.paddingRight = (box.clientWidth - (moveLen + resize.offsetWidth)) + 'px'; // 注意加上滚动条的宽度
    
          };
          // 鼠标松开事件
          document.onmouseup = function (evt) {
            //颜色恢复
            // resize.style.background = '#d6d6d6';
            document.onmousemove = null;
            document.onmouseup = null;
            resize.releaseCapture && resize.releaseCapture(); //当你不在需要继续获得鼠标消息就要应该调用ReleaseCapture()释放掉
          };
          resize.setCapture && resize.setCapture(); //该函数在属于当前线程的指定窗口里设置鼠标捕获
          return false;
        };
    
      }
      function rightDragControllerDivVisiable(main = "#weiMain", sidebar = "#weiRight", resize = "#weiRightResize", content = "#weiContent", minleft = 0, minright = 0) {
        // 层次结构:main 》 sidebar = resize = content
        var right = document.querySelector(sidebar);          // 侧边栏
        var mid = document.querySelector(content);           // 内容区域
        var box = document.querySelector(main);              // 整个大区域
        var resize = document.querySelector(resize);  // 拖动条
        if (mid.style.paddingRight == minright + 'px') {
          const root = document.documentElement;
          const rightWidth = window.getComputedStyle(root).getPropertyValue('--rightWidth');
          var maxT = box.clientWidth - rightWidth 
          console.log( box.clientWidth , rightWidth);
          resize.style.right = right.style.width =   mid.style.paddingRight =rightWidth  ; // 设置拖动条的位置
        } else {
          resize.style.right = right.style.width =   mid.style.paddingRight =0 ; // 设置拖动条的位置
        }
    
     
      }
    
      leftDragControllerDiv();
      rightDragControllerDiv();
    </script>
    <style>
    
    </style>
    
    • 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
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
  • 相关阅读:
    Springboot集成HBase使用案例
    Git GUI使用笔记
    VS和QT联合开发
    股票推荐系统,并查集
    深入探索:Facebook如何重塑社交互动
    标准C++day3——构造、析构函数和初始化列表
    RabbitMQ部署指南
    前后端分离 + 跨域 + 跨域的解决办法
    第七章 数学 2 AcWing 1545. 质因子
    面试官:设计模式中的适配器模式是什么?
  • 原文地址:https://blog.csdn.net/weixin_44797182/article/details/132744927