MarginLon MarginLon
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

MarginLon

http://info.cern.ch
首页
  • Web

    • Web基础
    • Web布局
    • CodeSnippet
  • Vue

    • Vue
  • React

    • React
  • Node.js

    • Node.js
  • 技术文档
  • UI组件库
  • 动画库
  • 图形库
  • 桌面端
  • 学习笔记
  • 生活笔记
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Web基础

    • CSS技巧收藏
    • 响应式设计
    • 常用meta整理
    • CSS滚动条scrollbar
    • CSS超链接标签a
    • CSS裁剪clip-path
    • CSS表格table
    • CSS机制@At-rule
    • CSS清除浮动
    • CSS预处理器
    • CSS选择器
    • CSS布局模型
    • CSS长度
    • CSS颜色
    • 左右分栏宽度拉伸调整
      • 代码实现
      • 特殊情况
  • Web布局

  • CodeSnippet

  • Vue

  • React

  • 前端
  • Web基础
MarginLon
2022-09-15
目录

左右分栏宽度拉伸调整

# 代码实现

<div class="container" id="container">
    <!--拖动区域-->
    <section class="drag-area" id="drag-area"></section>
    <!--拖动按钮-->
    <div class="drag-tip" id="drag-tip"></div>
  </div>
1
2
3
4
5
6
    .container {
      position: absolute;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      width: 100%;
      height: 100%;
      background-color: rgba(5, 9, 38, .8);
    }

    .drag-area {
      width: 300px;
      /*设置拖拽盒子的最小值,当拖拽的值小于这个值,将不再因拖拽而继续变小*/
      min-width: 100px;
      height: 100%;
      background-color: rgba(55, 165, 165, 0.3);
      position: relative;
    }

    .drag-tip {
      height: 100%;
      width: 6px;
      position: absolute;
      top: 0;
      left: 294px;
      background-color: transparent;
    }

    /*鼠标移入显示可拖动样式*/
    .drag-tip:hover {
      cursor: w-resize;
    }
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
let dragBtn = document.getElementById('drag-tip'),
      dragDom = document.getElementById('drag-area'),
      container = document.getElementById('container');

    // 鼠标摁下
    dragBtn.onmousedown = function (e) {
      // 鼠标的X轴坐标
      let clientX = e.clientX;
      // 拖动块距离屏幕左侧的偏移量
      let offsetLeft = dragBtn.offsetLeft;

      // 鼠标移动
      document.onmousemove = function (e) {
        let curDist = offsetLeft + (e.clientX - clientX), // 拖动块的移动距离
          maxDist = container.clientWidth - dragBtn.offsetWidth; // 拖动块的最大移动距离

        // 边界值处理
        curDist < 0 && (curDist = 0);
        curDist > maxDist && (curDist = maxDist);

        // 设置值(保证拖动块一直在拖动盒子的相对位置)
        dragBtn.style.left = dragDom.style.width = curDist + "px";
        return false
      };
      // 鼠标松开
      document.onmouseup = function (e) {
        document.onmousemove = null;
        document.onmouseup = null;
        // 释放鼠标
        dragBtn.releaseCapture && dragBtn.releaseCapture()
      };
      // 捕获鼠标
      dragBtn.setCapture && dragBtn.setCapture();
      return false
    };
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

# 特殊情况

  • 遇到 iframe 标签时可以采用如下方案: (但是无法在iframe内导航)
iframe{
  position:relative;
  z-index:-1;
}
1
2
3
4
编辑 (opens new window)
上次更新: 2023/09/17, 23:41:49
CSS颜色
CSS布局

← CSS颜色 CSS布局→

最近更新
01
KnockoutJS
11-12
02
综述
10-17
03
前言
10-12
更多文章>
Theme by Vdoing | Copyright © 2019-2024 MarginLon | MIT License
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式